Insert new values into an array

前端 未结 1 942
误落风尘
误落风尘 2021-01-25 00:46

I currently have a column vectors of different lengths and I want to insert another column vector at various points of the original array. i.e. I want to add my new array to the

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 01:17

    here's one way to do it:

    a = [1:30]';
    b = [0;0;0;0;0];
    
    a=reshape(a,10,[]);
    b=repmat(b,[1 size(a,2)])
    r=[b ; a]
    r=r(:);
    

    the trick is to reshape a to a matrix with columns of the right size (10 elements each). Replicate b to this # of columns , concatenate both and flatten the matrix to a vector...

    0 讨论(0)
提交回复
热议问题