Another Issue with 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
2 7
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' ' v' },'Type','outer','MergeKeys',true);
And the result is
C = x v
1 5
2 6
2 7
3 7
4 8
Where I want it to be:
C = x v
1 5
2 6
3 7
4 8
Thus specifying that if there are values with the same key present in A and B, I want the value of B to be in the matrix of C. I hope you understand the issue, and can help me with the answer.
Thanks in advance!

采纳的回答

Tom Lane
Tom Lane 2012-6-14
Your desired result has v=6 for x=2, but A and B provide different values for v in that case. One thing you could do is
join(A,B,'key','x','type','outer','mergekeys',true)
but this would leave you with one output v variable for each input, and you'd have to reconcile how you wanted to choose from one or the other.
  2 个评论
Roy Veldhuizen
Roy Veldhuizen 2012-6-14
Thank you for your answer! I used this method, removing values that i did not wanted to return in the C matrix beforehand. This did the job, but from your answer I conclude it is not possible to use the join function with a ' preference' for a certain dataset?
Tom Lane
Tom Lane 2012-6-14
Play with this, and see if it gets you anywhere:
[C,IA,IB] = join(A,B,'key','x','type','outer','mergekeys',true,'leftvars','v','rightvars',{})
C.v(IB>0) = B.v(IB(IB>0))
I take v from the A dataset, but then I overwrite values based on the B dataset. I'm not sure if this is exactly what you want, but maybe you can work with it.

请先登录,再进行评论。

更多回答(0 个)

类别

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