Star
Star

Reputation: 2299

How to compute frequency of rows in Matlab for submatrices?

I have a matrix D in Matlab. It is a concatenation of 4 matrices of 3 rows (breaks added for clarity). I would like to determine the number of unique rows within the total matrix and list them out for each of the sub-matrices with a count of how many times they occur.

D=[1 0 1 1; 
   0 1 1 1; 
   1 1 0 1; 
   --------
   1 1 0 1; 
   1 1 0 1; 
   0 1 1 1; 
   --------
   1 1 1 0; 
   0 1 1 1; 
   1 0 1 1;
   -------- 
   1 0 1 1; 
   1 0 1 1; 
   1 1 0 0]

So for the above matrix, there are 5 unique rows:

   1 0 1 1 
   0 1 1 1 
   1 1 0 1
   1 1 1 0 
   1 1 0 0 

So breaking those 5 rows into the 4 sub-matrices with counts of occurrence within them it would be:

C=[1 0 1 1  1; 
   0 1 1 1  1; 
   1 1 0 1  1; 
   1 1 1 0  0; 
   1 1 0 0  0; 
   --------
   1 0 1 1  0; 
   0 1 1 1  1; 
   1 1 0 1  2; 
   1 1 1 0  0; 
   1 1 0 0  0; 
   ----------
   1 0 1 1  1; 
   0 1 1 1  1; 
   1 1 0 1  0; 
   1 1 1 0  1; 
   1 1 0 0  0; 
   ----------
   1 0 1 1  2; 
   0 1 1 1  0; 
   1 1 0 1  0; 
   1 1 1 0  0; 
   1 1 0 0  1]

Upvotes: 2

Views: 181

Answers (3)

Luis Mendo
Luis Mendo

Reputation: 112749

If D only contains zeros and ones: you can interpret each row as a binary number and then use accumarray to count ocurrences, as follows:

nr = 3; %// number of rows of each submatrix
Db = bin2dec(char(D+'0'))+1; %// convert each row into a binary number, plus 1
R = accumarray([Db ceil(1/nr:1/nr:size(D,2)).'], 1, [], [], [], true); %'// count
%// occurrences. Sparse to save memory. Each col is a submatrix
[u v] = unique(Db,'stable'); %// unique values
R = reshape(R(u,:), [], 1); %// get only results for values that are present
C = [repmat(D(v,:), size(D,1)/nr, 1) full(R)]; %// build result

Upvotes: 2

Divakar
Divakar

Reputation: 221664

Code

%// Cut into a 3D array after every n rows
mat3d = permute(reshape(D,n,size(D,1)/n,[]),[1 3 2])

%// Get unique rows
unqmat = unique(D,'rows','stable')

%// Find equalities
eqs = bsxfun(@eq,mat3d,permute(unqmat,[4 2 3 1]))

%// Get counts
counts = squeeze(sum(all(eqs,2),1)).' %//'

%// Restructure to get the desired output
C = [repmat(unqmat,[m 1]) counts(:)]

Seems like older MATLAB versions don't have unique(..'rows','stable') capability that keeps the row order. For the same, here is a suggested replacement that is tested as well -

function out = unique_rows_stable(A)

[unqmat_notinorder,row_ind] = unique(A,'rows');
[~,ordered_rowind] = sort(row_ind);
out = unqmat_notinorder(ordered_rowind,:);

return;

Thus, the earlier solution code could be edited to have unqmat = unique_rows_stable(D) instead.

Upvotes: 3

CodeMonkey
CodeMonkey

Reputation: 1835

FYI, a question like this would normally be shut down since it doesn't explain what you've tried and what you're actually struggling with. I just thought it sounded like a fun exercise and I haven't written in Matlab for a while so I thought I'd give it a go. Hopefully it helps!

D=[1 0 1 1; 
   0 1 1 1; 
   1 1 0 1; 
   1 1 0 1; 
   1 1 0 1; 
   0 1 1 1; 
   1 1 1 0; 
   0 1 1 1; 
   1 0 1 1;
   1 0 1 1; 
   1 0 1 1; 
   1 1 0 0]

rows = [];
for i = 1:length(D)
    skip = 0;
    for j = 1:size(rows,1)
        if all(rows(j,:) == D(i,:))
            skip = 1;
        end
    end
    if ~skip
        rows = [rows; D(i,:)];
    end
end
C = [];
for i = 1:4
    for j = 1:length(rows)
        count = 0;
        for k = 1:3
            if all(D((i-1)*3+k,:) == rows(j,:))
                count = count + 1;
            end
        end
        C = [C; [rows(j,:) count]];
    end
end

C

Upvotes: 1

Related Questions