How to reshape array based on another array with nan values
7 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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
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
2020-11-2
编辑:Abolfazl Chaman Motlagh
2020-11-2
or just
b=logical(b);
A=nan(size(b));
A(b)=a;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!