Comparing Dataset

Hi everyone,
I'm a newbie in matlab.
I have two big datasets with different number of row but same number in column. I need to compare each element of both datasets with reference of column #1. If column #1 in both data set has exactly the same value then subtract column 2 in 1st dataset with 2nd dataset.
Let says:
dataset1 = [1 2; 2 2; 3 5; 4 2; 5 10]; dataset2 = [1 4; 3 2; 5 2];
and the result that I want is:
result = [1 -2; 3 3; 5 8]
How can I solve this? Thanks in advance.

 採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 9 日

0 投票

Use the three-output version of ismember to figure out which columns match which.

4 件のコメント

Mr D
Mr D 2012 年 5 月 9 日
Hi Walter,
Thanks for fast reply..Yes, I can define the correlation between two dataset, but when I try to do the operation between these datasets, it doesn't give me the result what I want.
Here my if statement:
dataset1 = [1 2; 2 2; 3 5; 4 2; 5 10];
dataset2 = [1 4; 3 2; 5 2];
is_mem = ismember(dataset1(:,1),dataset2(:,1));
delta = zeros(size(dataset1(:,1)));
for i =1:size(dataset1)
if (is_mem(i,1) == 1);
delta(i,1) = dataset1 (i,2) - dataset2 (i,2);
end
end
Could you give me a suggestion about this?
Thanks
Walter Roberson
Walter Roberson 2012 年 5 月 9 日
You only used the single-output version of ismember(). Please read the documentation for ismember() and look at the additional outputs that are available.
Mr D
Mr D 2012 年 5 月 9 日
I modified my code,
dataset1 = [1 2; 2 2; 3 5; 4 2; 5 10];
dataset2 = [1 4; 3 2; 5 2];
[is_mem,loc] = ismember(dataset2(:,1),dataset1(:,1));
delta = zeros(size(dataset2(:,1)));
for i = 1:size(dataset2)
for j= 1: size(dataset1)
if (loc(i,1) ~= 0);
delta(i,1) = dataset1 (j,2) - dataset2 (i,2);
end
end
end
and the result is still not what I want.
delta = [6;8;8]
the delta what I want is [-2,3,8].
What's wrong with my code?I'm really newbie in coding, could you give me more clear solution?
Thanks.
Mr D
Mr D 2012 年 5 月 9 日
Hi Walter,
Finally I can get the result what I want in simple code:
dataset1 = [1 2; 2 2; 3 5; 4 2; 5 10];
dataset2 = [1 4; 3 2; 5 2];
[is_mem,loc] = ismember(dataset2(:,1),dataset1(:,1));
v = dataset1(loc,2);
delta = dataset1(loc,2) - dataset2(:,2);
Thanks

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

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by