How to modify this "for" loop so it can read maximum Y value instead of X?

2 Ansichten (letzte 30 Tage)
Hi,
I have this code that reads maximum X value and its corresponding Y value, and prints all results (Y values) in one file.
I just need to modify this code, so it can read the maximum Y value.
For example, for the following set of data:
X Y
1 2
2 16
3 9
4 15
The code reads 4 (as the maximum X value) and prints 15 (as the corresponding Y value to the maximum X).
However, I need the modified code to just read and print 16 (as the maximum Y value).
The code:
S = dir('*.out');
C = natsortfiles({S.name});
N = numel(C);
g = 10;
Z = nan(g,1);
for k = 1:N/g
for j = 1:g;
data = load(C{10*(k-1)+j});
[~,idx] = max(data(:,1));
Z(j) = data(idx,2);
end
dlmwrite(['Result_',num2str(k),'.txt'],Z);
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 4 Mär. 2019
Bearbeitet: Walter Roberson am 4 Mär. 2019
max(data(:,2)) instead of max(data(:,1)) . And on the next line assign from data(idx,1) instead of data(idx,2) if you want the corresponding X value, or leave it as-is if you want the y value.
  3 Kommentare
Walter Roberson
Walter Roberson am 4 Mär. 2019
S = dir('*.out');
C = natsortfiles({S.name});
N = numel(C);
g = 10;
for k = 1:N/g
Z = nan(g,1);
for j = 1:g;
data = load(C{10*(k-1)+j});
Z(j) = max(data(:,2));
end
dlmwrite(['Result_',num2str(k),'.txt'],Z);
end
Ismail Qeshta
Ismail Qeshta am 4 Mär. 2019
Bearbeitet: Ismail Qeshta am 4 Mär. 2019
Hi Walter,
Great. Now it works well. Thank you very much.
Yes. It is acutally the second line. It should be Z(j) = data(idx,2) instead of Z(j) = data(idx,1) because we need to read and print Y not X.
Thank you again for your time and help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings 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