Issues with function Join
Show older comments
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
More Answers (1)
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.
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!