I have a matlab problem where I need to find the maximum number in a matrix, and then find the next greatest value in the matrix that is not in the same row or column as the previous one.
My thought process is that I will find the maximum value in the matrix and then figure out which row and column it is in and then set the rest of the values in the row and column to 0. so far I have this.
a=rand(5)
[row,column]=find(a==max(max(a)))
I can find which row and column the maximum is but that is about it. Can somebody help me with the next step or a better way to go about writing this program? Thank you!
Solution StepsDefine two variables firstRow and firstColumn to store the status of the first row and first column. Set them to false. Use these row and column as hash that stores the status of that row and column. Iterate over the matrix and for every A[i][j] == 0, set A[i][0] = 0 and col[0][j] = 0.
The easiest way to remove a row or column from a matrix is to set that row or column equal to a pair of empty square brackets [] . For example, create a 4-by-4 matrix and remove the second row.
What you need is:
a(row,:)=0;
So, in total:
a=rand(5)
[row,column]=find(a==max(max(a)))
a(row,:)=0;
[row2,column2]=find(a==max(max(a)))
if you have negatives values in a
, you can also do:
a(row,:)=-inf;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With