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

In summary, the conversation is about finding the best three rows or columns in a binary matrix using MATLAB. This can be done by counting the number of 1's in each row and column, sorting them, and then using the "find" command to locate the row and column with the highest number of 1s. The provided code demonstrates this process.
  • #1
imtiazgul
1
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
  • #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
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.
 

1. How do I find the best row/column in a binary matrix using MATLAB?

To find the best row/column in a binary matrix using MATLAB, you can use the max function with the [],[],2 syntax. This will return the maximum value in each row of the matrix, allowing you to determine the best row based on the highest value. Similarly, you can use the max function with the [],[],1 syntax to find the best column in the matrix.

2. Can I use a loop to find the best row/column in a binary matrix using MATLAB?

Yes, you can use a loop to find the best row/column in a binary matrix using MATLAB. You can iterate through each row or column and use the max function to find the maximum value in each row/column. Then, you can compare these values to determine the best row/column.

3. How do I handle ties when finding the best row/column in a binary matrix using MATLAB?

If there are ties for the maximum value in a row or column, the max function will return the index of the first occurrence of the maximum value. To handle ties, you can use the find function to return the indices of all occurrences of the maximum value, and then choose the best row/column based on your criteria.

4. Can I find the best row/column in a non-binary matrix using MATLAB?

Yes, you can find the best row/column in a non-binary matrix using MATLAB. However, the methods mentioned above will only work for binary matrices. For non-binary matrices, you can use the max function with the ,[],2 or ,[],1 syntax to find the maximum value in each row or column, respectively. Then, you can compare these values to determine the best row/column.

5. Is there a built-in function in MATLAB to find the best row/column in a binary matrix?

No, there is not a specific built-in function in MATLAB to find the best row/column in a binary matrix. However, as mentioned before, you can use the max function with the appropriate syntax to achieve this. Additionally, there may be other functions or methods available in MATLAB's toolboxes that can help you find the best row/column in a binary matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
804
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Precalculus Mathematics Homework Help
Replies
32
Views
688
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
742
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Precalculus Mathematics Homework Help
Replies
1
Views
448
Back
Top