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개)

카테고리

도움말 센터File Exchange에서 Phased Array Design and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by