How to reshape array based on another array with nan values

조회 수: 7 (최근 30일)
Michael Burnett
Michael Burnett 2020년 11월 2일
댓글: Michael Burnett 2020년 11월 2일
I want to see if there is a specific function that will help me do this:
I have a 10x1 array.
a = [2 6 5 4 7 2 3 2 3 1];
I would like to reshape it to fit the same shape as logical array b (with nan values as 0).
b = [1 1 1 0;1 1 1 1;1 0 1 1]
Is there an easy way to reshape my array in that mold?

채택된 답변

Bruno Luong
Bruno Luong 2020년 11월 2일
편집: Bruno Luong 2020년 11월 2일
a = [2 6 5 4 7 2 3 2 3 1]
b = [1 1 1 0;1 1 1 1;1 0 1 1]
A = nan(size(b'));
A(b'==1) = a;
A = A.'

추가 답변 (1개)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2020년 11월 2일
b=logical(b);
A=zeros(size(b));
A(b)=a;
A(~b)=nan(sum(~b,'all'),1);
output:
A =
2 4 2 NaN
6 7 3 3
5 NaN 2 1
  댓글 수: 2
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2020년 11월 2일
편집: Abolfazl Chaman Motlagh 2020년 11월 2일
or just
b=logical(b);
A=nan(size(b));
A(b)=a;
Michael Burnett
Michael Burnett 2020년 11월 2일
Lol, I'm quite new to MATLAB, so this is very helpful. Thanks

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by