Clear Filters
Clear Filters

Issues with function Join

2 views (last 30 days)
Roy Veldhuizen
Roy Veldhuizen on 1 Jun 2012
Hello everybody,
I'm trying to merge two datasets. One calculated, and one retrieved from data, given by:
A=dataset(Output(:,2),Output(:,1),'VarNames',{'x' 'v'});
B=dataset(datarest(:,x),datarest(:,v),'VarNames',{'x' 'v'});
which results in something in the form of:
A = x v
1 5
4 8
B = x v
2 6
3 7
In this, I want to use the x as keys, and merge it to get a dataset, which states v for every x. For this i'm using
C=join(A,B,'key','x','Type','outer','MergeKeys',true);
And the result is
C = x vleft vright
1 5 NaN
2 NaN 6
3 NaN 7
4 8 NaN
Where I want it to be:
C = x v
1 5
2 6
3 7
4 8
I guess it is possible to filter out the NaN's after the join, but as running time is an issue, i'm wondering if there is a way to do specify it within the join command immediately? I hope the issue is clear, and you can help me out.
Thanks in advance!

Accepted Answer

Oleg Komarov
Oleg Komarov on 1 Jun 2012
Try
C=join(A,B,'key',{'x','v'},'Type','outer','MergeKeys',true);
  2 Comments
Roy Veldhuizen
Roy Veldhuizen on 1 Jun 2012
This is exactly what i was looking for. Thank you very much.
But I do not really understand the syntax. I guess, this means that the join function uses both x and v as keys, but I'm not really sure how this results in the
C = x v
1 5
2 6
3 7
4 8
Oleg Komarov
Oleg Komarov on 1 Jun 2012
I tried to think about an example but I really can't come with something intuitive. NaNs are placeholders for features that belong to one set and not the other. In your first results you could join all x and see which set the feature came from, now if you identify a unique entry with two features and merge them, then you loose information about which set it came from.

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 1 Jun 2012
Roy, in your simple example, where the key variable is unique "within" and disjoint "between", the best way to get what you want is just
[A; B]
But it may be that you are using a full outer join because your real problem is not that simple. In general, there's no reason why "v" in one array means the same thing as "v" in the other, and even if they do, automatically merging would probably only be a good idea if the key is disjoint "between". So JOIN does not try to merge data variables with the same names. In your simple example, it's easy to do:
leftNaNs = isnan(C.vleft);
C.v = C.vleft;
C.v(leftNaNs) = c.vright(leftNaNs);
C.vleft = []; C.vright = [];
but in general it would take more care.
Hope this helps.

Products

Community Treasure Hunt

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

Start Hunting!