How do you arrange a column vector so it has the same shape of a predefined matrix with some NaN elements??

1 view (last 30 days)
Hi :)
I have a mxn Matrix, say A. Some of its elements are empty, something like this:
A=[1, 2, 3, 4;
NaN,NaN,5, 6;
7, 8, NaN,NaN];
I also have a column vector, say B. Suppose it also has 8 numerical elements, something like this:
B=[20;21;22;23;24;25;26;27]; % Column vector.
I'm stuck trying to arrange B as a new matrix R, but I want R to have the same format (shape) as A, so it looks something like this:
R=[20, 21, 22, 23;
NaN, NaN, 24, 25;
26, 27, NaN, NaN];
Any help with this is appreciated. Thanks in advance.

Accepted Answer

Star Strider
Star Strider on 3 Oct 2014
This works:
A=[1,2,3,4; NaN,NaN,5,6; 7,8,NaN,NaN];
B=[20;21;22;23;24;25;26;27];
R = NaN(size(A'));
R(~isnan(A')) = B;
R = R';

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!