Issues with function Join

1 次查看(过去 30 天)
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!

采纳的回答

Oleg Komarov
Oleg Komarov 2012-6-1
Try
C=join(A,B,'key',{'x','v'},'Type','outer','MergeKeys',true);
  2 个评论
Roy Veldhuizen
Roy Veldhuizen 2012-6-1
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 2012-6-1
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.

请先登录,再进行评论。

更多回答(1 个)

Peter Perkins
Peter Perkins 2012-6-1
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by