Comparing Dataset

2 views (last 30 days)
Mr D
Mr D on 9 May 2012
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.

Accepted Answer

Walter Roberson
Walter Roberson on 9 May 2012
Use the three-output version of ismember to figure out which columns match which.
  4 Comments
Mr D
Mr D on 9 May 2012
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 on 9 May 2012
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

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!