Issues with function Join

조회 수: 1 (최근 30일)
Roy Veldhuizen
Roy Veldhuizen 2012년 6월 1일
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