For loop stops in the first find

3 Ansichten (letzte 30 Tage)
Fil Okua
Fil Okua am 25 Apr. 2021
Bearbeitet: Walter Roberson am 25 Apr. 2021
Why does this for loop stop at the first find?.
Val = max(max(D(:, 2:end), [], 2));
rows = [];
for i = 1:size(D, 1)
if (any(D(i, 2:end) == Val))
rows = [rows, i];
D(rows)
end
end
  5 Kommentare
Fil Okua
Fil Okua am 25 Apr. 2021
@Image Analyst, I am trying store both.
Fil Okua
Fil Okua am 25 Apr. 2021
But I'm only interested in the values at the indexes

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 25 Apr. 2021
Bearbeitet: Walter Roberson am 25 Apr. 2021
Val = max(max(D(:, 2:end), [], 2));
rows = [];
for i = 1:size(D, 1)
if (any(D(i, 2:end) == Val))
rows = [rows; D(i,:)];
end
end
However...
mask = any(D == max(max(D(:,2:end))),2);
rows = D(mask,:);
with no loop is all that is needed.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements 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!

Translated by