How to combine arrays using NaN?

조회 수: 14 (최근 30일)
Sean O'Donnell
Sean O'Donnell 2018년 12월 12일
댓글: Sean O'Donnell 2018년 12월 13일
I have two arrays, one longer than the other. The long array is sequential from 1 to some value, with NaN filling some elements instead of the value of that element. The length of the short array is the length of the long array without the NaN elements. How do I combine the arrays so that the new array will be the same length as the long array, and will have NaN in the same elements as in the long array, and the data from the short array in the rest of the elements?
Ex:
A = [1 2 3 NaN 5 6 NaN 8 NaN 10];
B = [96 12 83 71 34 64 21];
FinalArray = [96 12 83 NaN 71 34 NaN 64 NaN 21];
Thanks

채택된 답변

Daniele Mascali
Daniele Mascali 2018년 12월 12일
One possible solution:
FinalArray = nan(size(A));
FinalArray(find(not(isnan(A)))) = B
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 12월 12일
More compactly:
FinalArray = nan(size(A));
FinalArray(~isnan(A)) = B;
Sean O'Donnell
Sean O'Donnell 2018년 12월 13일
Thank you. That worked.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by