A logical indexing for row-wise max
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
cyril
am 12 Apr. 2014
Beantwortet: Azzi Abdelmalek
am 12 Apr. 2014
I'm wondering if there is anything simpler than:
x = [2 3;3 1]
[a,b]=max(x, [], 2);
% now return the matrix containing row-wise max logical indexes, in this example [0 1;1 0]
result = zeros(size(x));
for i=1:size(x,1)
result(i,b(i)) = 1;
end
I had thought to use
result = x==repmat(a,1,size(x,2))
But I'm afraid about floating-point issues
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 12 Apr. 2014
x = [2 3;3 1]
[a,b]=max(x, [], 2);
result = zeros(size(x));
result(sub2ind(size(x),1:size(x,1),b'))=1
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!