Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jun 1, 2010 #1
    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: Jun 1, 2010
  2. jcsd
  3. Jun 1, 2010 #2
    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
    for i=1:c
    nc(i)=sum(a(:,i)); %find no of 1s in each column
    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
    if length(br1)==2
    if length(br1)==3

    Now br1,br2 and br3 are best three rows. Repeat the same to find best three columns.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook