MATLAB Using matlab how to find best row/column in a binary matrix

AI Thread Summary
To identify the rows and columns with the highest number of ones in a binary matrix using MATLAB, the process involves counting the ones in each row and column, sorting these counts, and then locating the top three. The "sum" command is utilized to calculate the number of ones per row and column. After obtaining the counts, they are sorted, and the "find" command is employed to determine the indices of the rows and columns with the highest counts. The provided MATLAB code outlines the steps: it initializes the matrix, counts the ones, sorts the results, and identifies the top three rows and columns while handling potential ties in counts. This method ensures an efficient and straightforward approach to analyzing binary matrices.
imtiazgul
Messages
1
Reaction score
0
Hello all
i need help in finding the best row/column in a binary matrix.
suppose i have a matrix of size 100*50 containing only 1s and 0s, how can i find (using matlab)which row or column has highest number of ones? zeros and ones are randomly distributed in the matrix.
i want to find best three rows or coloumns in the matrix
 
Last edited:
Physics news on Phys.org
Count the number of 1's in each row and column. Since you have only binary values, you can use "sum" command. Sort the rows and columns. Using "find" command find the row and column that contains the highest number of 1s
Let a be the given matrix

clear all
r=size(a,1); %find no. of rows
c=size(a,2); %find no. of columns
for i=1:r
nr(i)=sum(a(i,:)); %find no of 1s in each row
end
for i=1:c
nc(i)=sum(a(:,i)); %find no of 1s in each column
end
nrs=sort(nr); % sorting row
ncs=sort(nc); %sorting column
% finding best three rows
br1=find(nr==nrs(r)); %1st best row
if length(br1)==1
br2=find(nr==nrs(r-1));%2nd best row
if length(br2)==1
br3=find(nr==nrs(r-2));%3rd best row
else % remaining lines are meant to overcome the multiple rows of same highest 1s
br3=br2(1,2); % I hope it is self understandable refer help of find command
br2=br2(1,1);
end
end
if length(br1)==2
br2=br1(1,2);
br1=br1(1,1);
br3=find(nr==nrs(r-2));
end
if length(br1)==3
br2=br1(1,2);
br3=br1(1,3);
br1=br1(1,1);
endNow br1,br2 and br3 are best three rows. Repeat the same to find best three columns.
 
Back
Top