Easy columns merge question
Afficher commentaires plus anciens
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!
Réponses (2)
Azzi Abdelmalek
le 6 Juin 2016
Modifié(e) : Azzi Abdelmalek
le 6 Juin 2016
A = [1 NaN 3 NaN 5 6]
B= [NaN 2 NaN 4 5 6]
C=unique([A B])
C(isnan(C))=[]
3 commentaires
012786534
le 6 Juin 2016
Azzi Abdelmalek
le 6 Juin 2016
Your original question is different. Please edit your question
Azzi Abdelmalek
le 6 Juin 2016
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
le 6 Juin 2016
Try this:
C = A; % Initialize
% Replace any nan's with B
C(isnan(C)) = B(isnan(C))
3 commentaires
Image Analyst
le 6 Juin 2016
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
le 6 Juin 2016
Image Analyst
le 7 Juin 2016
Modifié(e) : Image Analyst
le 7 Juin 2016
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
Catégories
En savoir plus sur NaNs dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!