2017-05-04 2 views
0

Ich versuche, die Array-Werte in einiger bestimmten Zeile (Zeilennummer 1, 10, 12, 20, 39 usw., nicht kontinuierlich) zu ersetzen den linearen Index aus find Verwendung . Aber ich weiß nicht, wie man nach diesen paar Zeilen Code weitergeht:bei einigen speziellen Zeilen in Matlab

[valmax, ~]=max(A); %Where A will consist of more than one MAX value 
idxmax=find(A==valmax); 
mclr=repmat([1 0 0],[10 1]); %Create the matrix of my value 
mclr(idxmax,:)=[0 1 0]; %replace the value at idxmax index, this line won't work 

Irgendeine Idee, wie man das repariert? Oder gibt es andere Funktion anstelle von find? Danke!

Antwort

2

können Sie ind2sub verwenden, um die linearen Indizes in Zeilenindizes zu konvertieren:

A = randi(5,[10 3]); % random matrix 
[valmax, ~] = max(A(:)); %Where A will consist of more than one MAX value 
idxmax = find(A == valmax); 
% convert linear index into row index 
[rowmax,colmax] = ind2sub(size(A),idxmax); 
rowmax = unique(rowmax); % get unique rows 
mclr = repmat([1 0 0],[10 1]); %Create the matrix of my value 
mclr(rowmax,:) = repmat([0 1 0],[numel(rowmax) 1]); %replace the value at idxmax index 

aber es effizienter ist direkt erhalten die Zeilen max Werte enthält any(X, 2) mit:

rowmax = find(any(A == valmax,2)); 
Verwandte Themen