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일

0 개 추천

A = [1 NaN 3 NaN 5 6]
B= [NaN 2 NaN 4 5 6]
C=unique([A B])
C(isnan(C))=[]

댓글 수: 3

012786534
012786534 2016년 6월 6일
Thank you for your great answer but what if I need to keep the NaNs when there are no values and keep the numbers in order? Like say A = [1 NaN NaN 4 5 NaN], B = [1 2 NaN NaN 5 NaN] so that C = [1 2 NaN 4 5 NaN]? Thanks!!!
Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 6일
Your original question is different. Please edit your question
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일

0 개 추천

Try this:
C = A; % Initialize
% Replace any nan's with B
C(isnan(C)) = B(isnan(C))

댓글 수: 3

Image Analyst
Image Analyst 2016년 6월 6일
Will you ever have A and B have different numbers in the same row? If so, which value does C take on: the value from A or the value from B?
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

태그

질문:

2016년 6월 6일

편집:

2016년 6월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by