from a vector of 2 create vector of length 9 with nans in matlab

To create a vector of length 9 with NaNs from a vector of length 2, you can use the nan function and repmat function. Here is the code to do that:

main.m
v = [1 2]; % example vector of length 2
out = repmat(NaN, 1, 9); % create a vector of length 9 with NaNs
out([1, 5]) = v; % insert the values at positions 1 and 5
163 chars
4 lines

What this code does is create a vector out of length 9 with all elements being NaNs using repmat. Then, it inserts the values from the original vector v into positions 1 and 5 using indexing notation.

The resulting vector out will have NaN values in all positions except the ones where the original vector was inserted:

main.m
out = [1 NaN NaN NaN 2 NaN NaN NaN NaN]
40 chars
2 lines

related categories

gistlibby LogSnag