Easy columns merge question

3 次查看(过去 30 天)
012786534
012786534 2016-6-6
Hi all, I have an easy merge question.Let's say I have 2 columns that look like this: A = [1 NaN 3 NaN 5 6 ...] and B = [NaN 2 NaN 4 5 6 ...]. Now, I want to merge them so I get C = [1 2 3 4 5 6 ...]. Any ideas? Thanks all!

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-6-6
编辑:Azzi Abdelmalek 2016-6-6
A = [1 NaN 3 NaN 5 6]
B= [NaN 2 NaN 4 5 6]
C=unique([A B])
C(isnan(C))=[]
  3 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2016-6-6
Your original question is different. Please edit your question
Azzi Abdelmalek
Azzi Abdelmalek 2016-6-6
A = [1 NaN NaN 4 5 NaN],
B = [1 2 NaN NaN 5 NaN]
i1=isnan(A)
i2=~isnan(B)
C=A
C(i1&i2)=B(i1&i2)

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-6-6
Try this:
C = A; % Initialize
% Replace any nan's with B
C(isnan(C)) = B(isnan(C))
  3 个评论
012786534
012786534 2016-6-6
Yes, the numbers may be the same. If so, C take the value of A
Image Analyst
Image Analyst 2016-6-7
编辑:Image Analyst 2016-6-7
Then my code works.
It works even if you meant "Yes, the numbers may be different. If so, C take the value of A"
If you wanted B numbers in preference to A, then initialize to B instead of A.
A = [1 NaN NaN 4 5 NaN]
B = [1 2 NaN NaN 5 NaN]
C = A; % Initialize
% Replace any nan's with B
C(isnan(C)) = B(isnan(C))
C =
1 2 NaN 4 5 NaN

请先登录,再进行评论。

类别

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