Creating matrix of maximum values indices in MATLAB

Posted by Gacek on Stack Overflow See other posts from Stack Overflow or by Gacek
Published on 2010-01-17T15:19:16Z Indexed on 2010/06/05 21:32 UTC
Read the original article Hit count: 262

Filed under:
|
|

Using MATLAB, I have an array of values of size 8 rows x N columns. I need to create a matrix of the same size, that counts maximum values in each column and puts 1 in the cell that contains maximum value, and 0 elsewhere.

A little example. Lets assume we have an array of values D:

    D =
      0.0088358   0.0040346   0.40276     0.0053221
      0.017503    0.011966    0.015095    0.017383
      0.14337     0.38608     0.16509     0.15763
      0.27546     0.25433     0.2764      0.28442
      0.01629     0.0060465   0.0082339   0.0099775
      0.034521    0.01196     0.016289    0.021012
      0.12632     0.13339     0.11113     0.10288
      0.3777      0.19219     0.005005    0.40137

Then, the output matrix for such matrix D would be:

    0    0    1    0
    0    0    0    0
    0    1    0    0
    0    0    0    0
    0    0    0    0
    0    0    0    0
    0    0    0    0
    1    0    0    1

Is there a way to do it without catching vector of indices from max function and then putting ones in the right place using for loop?

© Stack Overflow or respective owner

Related posts about matlab

Related posts about matrix