Hi I have m by n matrix,I want to find the index of the max. of each column.It can be found by [i j]:max(x,[],2).this gives the index of max of each column in vector i.I want if there is a repetion in vector i to be reset to 0.thanks

Hi I have m by n matrix,I want to find the index of the max. of each column.It can be found by [i j]:max(x,[],2).this gives the index of max of each column in vector i.I want if there is a repetion in vector i to be reset to 0.thanks

Antworten (1)

m=[1 2 3;0 5 6;2 1 7;0 4 9]
[ii,jj]=max(m)
%[ii ji]=max(x,[],2) gives the max of each row

6 Kommentare

Yes,there is no repition,but if the matrix is big ,there will be repitions in vector jj. try:
r=randi(500,10,20);
[ii jj]=max(r)
ii=483 486 467 476 398 480 480 465 459 468 398 499 481 456 427 473 479 411 391 465
jj=10 2 4 9 4 7 6 5 5 5 2 8 2 2 1 4 8 3 9 1
for example number 2 is repeated so manytimes
Ok, if 2 is repeated, what do you want to do?
Maybe you want
r=randi(500,10,20);
[ii jj]=max(r)
[kk,ee]=unique(jj,'stable') ;
idx=zeros(1,numel(jj));
idx(ee)=kk
Actually,you have done a big favor for me.I want to put zeros in case of repetitions. Thanks a lot I have just started with Matlab since 2 weeks and every time I stuck in some points. Thanks again
But when I use [ii jj]=max(r,[],2) instead of using [ii jj]=max(r).It does not work.Do you know why. I'm writing the max function in this way to find the max of each column.
No, you are looking for the max of each row, to find the maximum of each column
[ii ,jj]=max(r)
%or
[ii, jj]=max(r,[],1)

Diese Frage ist geschlossen.

Tags

Gefragt:

am 11 Nov. 2013

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by