Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i set a row within a matrix to 0?

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!

like image 678
user1068253 Avatar asked Nov 27 '11 19:11

user1068253


People also ask

How do you set a matrix to zero?

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.

How do you get rid of a row in a matrix?

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.


1 Answers

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;
like image 186
Oli Avatar answered Sep 21 '22 12:09

Oli