1
votes

I have a 133120x4 matrix in Matlab.

I would like to find the largest value in each row, and divide every element in that row by that particular value.

Do I need to use some sort of loop? For example: I find the amount of rows in that matrix (133120), and iterate the loop that amount of times, then I go row by row and use the max function to return the largest value in that row, and divide each element in that row by the returned value from max.

Or is there a quicker way of doing this?

Thanks

EDIT (for clarification):

lets call my 133120x4 matrix A. I want to divide every element in a row by the largest value in that row. Since max and element-division are vectorized, would the solution simply be:

A_normal = A / max(A)

resulting in a 133120x4 matrix, but within each row, the largest value would be 1.

Is this correct? EDIT: It is not correct, and am still trying to figure out solution. Help from the community is greatly appreciated

1
No, no loop required. Both max and element-wise division are vectorized.beaker
please see the update, would this be the solution? Thanks2step
@2step is there something impeding you from trying it?Ander Biguri
my solution results in a 133120x1 vector, not the desired 133120x4 matrix2step
@2step then, you should be quite ready to answer the question you added in the edit. Is that correct? No, its not.Ander Biguri

1 Answers

2
votes

Compute the maximum with max, repeat the result N(=4) times so there is one per each element and then element wise division !

newMat=mat./repmat(max(mat,[],2),[1 size(mat,2)]);]

or in R2016b or newer just

newMat=mat./max(mat,[],2);