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

  • Context: MATLAB 
  • Thread starter Thread starter imtiazgul
  • Start date Start date
  • Tags Tags
    Binary Matlab Matrix
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 7K views
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.