Finding common x values along with the corresponding y values.

1 回表示 (過去 30 日間)
Mr. 206
Mr. 206 2019 年 2 月 26 日
回答済み: Jan 2019 年 2 月 26 日
I have to sets of data, First set comprises of x_exp and y_exp. And second set comprises of x_sim and y_sim. They are as follows,
x_exp=[980;1000;1020;1040;1040];
y_exp=[0.003;0.10;0.020;0.008;0.002];
x_sim=[920;940;960;980;1000;1020;1040;1040];
y_sim=[0.005;0.003;0.0010;0.0025;0.0050;0.008;0.010;0.020];
I want to find the common values between x_exp and x_sim along with the corresponding y_sim values.
The result that I expect is
x_sim=[980;1000;1020;1040;1040];
y_sim=[0.0025;0.0050;0.008;0.010;0.020];

採用された回答

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019 年 2 月 26 日
Hi,
Try something like this,
t = 1;
for i =1:length(x_exp)
for j = 1:length(x_sim)
if x_exp(i) == x_sim(j)
x(t) = x_sim(j);
y(t) = y_sim(j);
t = t+1;
break
end
end
end
x and y are your results.
Regards

その他の回答 (1 件)

Jan
Jan 2019 年 2 月 26 日
[match, index] = ismember(x_exp, x_sim)
result = y_sim(index(match))
A problem is the repeated value:
x_exp = [1040; 1040]
x_sim = [1040; 1040];
y_sim = [0.010; 0.020];
ismember uses the first occurrence. If you want to use a found value once only, you have to write some special code, which considers this. Then start with Yasasvi Harish Kumar's solution and overwrite a found x_sim value.

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by