Problem in my for loop to find maximum y value

Hi, my for loop keeps repeating the same third value for the maximum index value. Can anyone please check it for me? the full code is:
for k = 1:3
Driftt = sprintf('Drift%d.out', k);
Reactt = sprintf('React%d.out', k);
matDrift = importdata(fullfile(Driftt));
matReact = importdata(fullfile(Reactt));
x= matDrift(:,2);
y= -sum(matReact(:,2:11),2);
plot(x,y)
hold on;
%
sort = zeros(N,2);
for i=1:N
[valy,idx] = max(y);
sort(i,:) = [x(idx,1),valy];
hold on;
end
end

2 件のコメント

Ismail Qeshta
Ismail Qeshta 2017 年 11 月 11 日
The problem is in this part:
sort = zeros(N,2);
for i=1:N
[valy,idx] = max(y);
sort(i,:) = [x(idx,1),valy];
hold on;
end
end
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 11 日
files = dir('*.out') ;
N = length(files) ;
sorted = zeros(N,2) ;
for i = 1:N
data = importdata(files(i).name) ;
[val,idx] = max(data(:,2)) ;
sorted(i,:) = [data(idx,1),val] ;
end
This code is similar to the one I am using. This code works well, but with the x and y data in the same file. In my case, I am plotting data from two separate files.

サインインしてコメントする。

 採用された回答

Jan
Jan 2017 年 11 月 11 日
編集済み: Jan 2017 年 11 月 11 日

1 投票

The code contains several problems:
1. fullfile(Driftt) does not do anything. Define the directory in addition:
Folder = cd;
...
matDrift = importdata(fullfile(Folder, Driftt));
2. Do not use "sort" as name of a variable, because this shadows the important function with the same name. This is not an error directly, but trying to use the function|sort()| afterwards produces unexpected behavior.
3. What is the value of "N"?
Perhaps you want:
sorted = zeros(N,2);
for i = 1:N
[valy, idx] = max(y(i, :)); % or y(:, i)?
sorted(i,:) = [x(idx,1), valy];
% hold on; Completely useless here, remove it
end
hold set the 'NextPlot' property of the current axes such that following plot commands do not remove existing line objects. But you are nor drawing here.
You could do this without a loop also:
[valy, idx] = max(y, [], 2); % or max(y, [], 1) ?
sorted = [x(idx, 1), valy];
Perhaps you have to modify the dimensions for the concatenation.
4. The variable "sort" (renamed to "sorted" in my code) is overwritten. Perhaps you want:
sorted = cell(1, 3);
...
[valy, idx] = max(y, [], 2); % or max(y, [], 1) ?
sorted{k} = [x(idx, 1), valy];

8 件のコメント

Ismail Qeshta
Ismail Qeshta 2017 年 11 月 11 日
Hi Jan,
Many thanks for your suggestions. They are really useful. I have done the necessary corrections, but I still can't get the code right. What do you mean by []? Sorry, I missed the value of N in my previous code. It is N=3.
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 11 日
I also tried your suggestions in points 3 and 4. In point 3, I only get one value for x(idx,1), and zeroes for the valy. In point 4, I get only one value while the other two are empty rectangular parentheses [].
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 11 日
編集済み: Ismail Qeshta 2017 年 11 月 11 日
files = dir('*.out') ;
N = length(files) ;
sorted = zeros(N,2) ;
for i = 1:N
data = importdata(files(i).name) ;
[val,idx] = max(data(:,2)) ;
sorted(i,:) = [data(idx,1),val] ;
end
This code is similar to the one I am using. This code works well, but with the x and y data in the same file. In my case, I am plotting data from two separate files.
Jan
Jan 2017 年 11 月 11 日
"max(y, [], 1)" searches along the first dimensions, "max(y, [], 2)" along the 2nd.
In which different files are your x and y data found?
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 11 日
Hi Jan, Thanks for your reply. The files are from: Drift1, Drift2, Drift3 and React1, React2, React3.
Jan
Jan 2017 年 11 月 12 日
編集済み: Jan 2017 年 11 月 12 日
If the data are contained in 2 files, import both files:
DriftFiles = dir('Drift*.out');
ReactFiles = dir('React*.out');
N = length(DriftFiles);
sorted = zeros(N, 2);
for k = 1:N
DriftData = importdata(DriftFiles(k).name) ;
ReactData = importdata(ReactFiles(k).name) ;
[val, idx] = max(DriftData(:, 2));
sorted(k,:) = [ReactData(idx, 1), val];
end
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 12 日
編集済み: Ismail Qeshta 2017 年 11 月 12 日
Hi Jan. Great. Thank you very much. It worked for x values. But for y values, I need to take the summation of columns (from 2 to 11) at each row. I am not sure where I shall put the code:
-sum(ReactData(:,2:11),2)
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 12 日
編集済み: Ismail Qeshta 2017 年 11 月 12 日
All Good. I could figure it out. Thanks Jan for all your help. You have been awesome.
The code now is:
DriftFiles = dir('Drift*.out');
ReactFiles = dir('React*.out');
N = length(DriftFiles);
sorted = zeros(N, 2);
for k = 1:N
DriftData = importdata(DriftFiles(k).name) ;
ReactData = importdata(ReactFiles(k).name) ;
Reactt = -sum(ReactData(:,2:11),2);
Driftt= DriftData(:,2);
[val, idx] = max(Reactt);
sorted(k,:) = [Driftt(idx, 1), val];
end

サインインしてコメントする。

その他の回答 (1 件)

Birdman
Birdman 2017 年 11 月 11 日
編集済み: Birdman 2017 年 11 月 11 日

2 投票

Because you keep overwriting Driftt and Reactt variables in the first for loop. When k=3, Driftt and Reactt arrays take their last shape and therefore you always see same maximum value.
Driftt = sprintf('Drift%d.out', k);
Reactt = sprintf('React%d.out', k);
This part causes the problem. At last when k=3, Driftt is equal to Drift3.out file and same for the other. Change this. You might need indexing.

2 件のコメント

Ismail Qeshta
Ismail Qeshta 2017 年 11 月 11 日
Thank you for the suggestion. I tried to remove the first for loop, but the problem is still occurring.
Jan
Jan 2017 年 11 月 11 日
編集済み: Jan 2017 年 11 月 11 日
No, Driftt nor Reactt are overwritten intentionally, because they are changing file names. There is no problem if the file name is "Drift3.out" for k=3.
The variable "sort" is overwritten repeatedly, so this might be the problem.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2017 年 11 月 11 日

編集済み:

Jan
2017 年 11 月 12 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by