Comparing Dataset

3 visualizzazioni (ultimi 30 giorni)
Mr D
Mr D il 9 Mag 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.

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by