getting a max of a matrix without repetition of index
2 views (last 30 days)
Show older comments
I have a matrix say A=[0.33 0.57 0.004 0.11; 0.39 0.01 1.00 0.003; 0.06 0.10 0.05 0.09;]
and I need max and its index of each row; which I type [MAC col]=max(A,[],2);
the results it gives me is MAC=[0.57 1.00 0.10] and col=[2 3 2].
What I need is to have col=[2 3 4] and MAC=[0.57 1.00 0.09] which has no repetition in the index "col". In this case maximum is unique for each column; if row 1 and row 3 have their maxima at a same column 2; I need MATLAB to detect which one is higher, take that as maxima of the corresponding row. for the other row the next maxima would be the answer. Does anybody know how I can do that without programming?
0 Comments
Accepted Answer
Sean de Wolski
on 18 Jul 2012
A=[0.33 0.57 0.004 0.11; 0.39 0.01 1.00 0.003; 0.06 0.10 0.05 0.09;];
sz1 = size(A,1);
m = zeros(sz1,1);
idx = m;
A2 = A;
for ii = 1:sz1
[m(ii), idx(ii)] = max(A2(ii,:));
A2(:,ii) = -inf;
end
3 Comments
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!