How to reshape array based on another array with nan values

7 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 2 Nov. 2020
Bearbeitet: Bruno Luong am 2 Nov. 2020
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.'

Weitere Antworten (1)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh am 2 Nov. 2020
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 Kommentare
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh am 2 Nov. 2020
Bearbeitet: Abolfazl Chaman Motlagh am 2 Nov. 2020
or just
b=logical(b);
A=nan(size(b));
A(b)=a;
Michael Burnett
Michael Burnett am 2 Nov. 2020
Lol, I'm quite new to MATLAB, so this is very helpful. Thanks

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by