How obtain the Y value that corresponds to the largest X value for a number of plots?

1 回表示 (過去 30 日間)
Ismail Qeshta
Ismail Qeshta 2019 年 2 月 18 日
Hi,
I am trying to write a code that gives me the Y value that corresponds to the largest X value for a number of plots.
I have a code that interpolates X values from Y. I just need the oposite and for the largest X values instead.
clear; clc;
Folder = cd;
N=170;
x2 = zeros(N, 10);
for k = 1:N;
Driftt = sprintf('Disp%d.out', k);
Reactt = sprintf('React%d.out', k);
matDrift = importdata(fullfile(Folder, Driftt));
matReact = importdata(fullfile(Folder, Reactt));
x1= matDrift(:,1);
y1= matDrift(:,2);
[x3, ix] = unique(x1);
y3 = y1(ix);
A=dlmread('Time.txt');
for i=1:size(A,2)
x2 (k,:) = interp1(y3, x3, A(:,i), 'linear');
temp=x2(k,:);
temp(isnan(temp))=0.05;
x2(k,:)=temp;
fid=fopen(['result_' num2str(i) '.txt'],'a');
fprintf(fid,'%f\n',x2(k,:));
fclose(fid);
end

回答 (2 件)

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019 年 2 月 18 日
編集済み: madhan ravi 2019 年 2 月 18 日
Hey,
I think the following should help. The variable big is an array of the largest x value in each row and the variable bigy is contains corresponding y values.
clear; clc;
Folder = cd;
N=170;
big = zeros(N);
bigy = zeros(N);
x2 = zeros(N, 10);
for k = 1:N;
Driftt = sprintf('Disp%d.out', k);
Reactt = sprintf('React%d.out', k);
matDrift = importdata(fullfile(Folder, Driftt));
matReact = importdata(fullfile(Folder, Reactt));
x1= matDrift(:,1);
y1= matDrift(:,2);
[x3, ix] = unique(x1);
y3 = y1(ix);
A=dlmread('Time.txt');
for i=1:size(A,2)
x2 (k,:) = interp1(y3, x3, A(:,i), 'linear');
temp=x2(k,:);
temp(isnan(temp))=0.05;
x2(k,:)=temp;
for z = 1:10
if x2(k,z)>big(z)
big(z) = x2(k,z);
bigy(z) = A(z,i);
end
end
fid=fopen(['result_' num2str(i) '.txt'],'a');
fprintf(fid,'%f\n',x2(k,:));
fclose(fid);
end
I hope I was of some help.
Regards
  2 件のコメント
Ismail Qeshta
Ismail Qeshta 2019 年 2 月 18 日
編集済み: Ismail Qeshta 2019 年 2 月 18 日
Hi Yasasvi,
Many thanks for your answer and suggested code. I actually keep getting the following error message:
Error using griddedInterpolant
The grid vectors must contain unique points.
Error in interp1 (line 151)
F = griddedInterpolant(X,V,method);
Error in Interpolation (line 44)
x2 (k,:) = interp1(y3, x3, A(:,i), 'linear');
Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019 年 2 月 18 日
To interpolate you will need unique y3 and x3 values. Is there any repeating value in x3 and y3?

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


KSSV
KSSV 2019 年 2 月 18 日
Read about the function max
Let X,Y be your data.
[max_x,idx] = max(X) ;
max_y = Y(idx) ;
  7 件のコメント
KSSV
KSSV 2019 年 2 月 18 日
YOu need not to use interp1..you got your maximum vlaues.
Ismail Qeshta
Ismail Qeshta 2019 年 2 月 18 日
編集済み: Ismail Qeshta 2019 年 2 月 18 日
Thanks. This is my latest code, but it still does not work. I get all zeroes in my output "Disp.txt" file.
clear; clc;
Folder = cd;
N=170;
x2 = zeros(N, 10);
for k = 1:N;
X = sprintf('SaadC2DynDisp%d.out', k);
Y = sprintf('SaadC2DynReact%d.out', k);
matDrift = importdata(fullfile(Folder, X));
matReact = importdata(fullfile(Folder, Y));
x1= matDrift(:,1);
y1= matDrift(:,2);
[max_x,idx] = max(X) ;
max_y = Y(idx) ;
[x3, ix] = unique(x1);
y3 = y1(ix);
A=dlmread('Time.txt');
fid=fopen(['Disp_' num2str(i) '.txt'],'a');
fprintf(fid,'%f\n',x2(k,:));
fclose(fid);
end

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by