插入新的值到一个数组 [英] Insert new values into an array

查看:91
本文介绍了插入新的值到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有不同长度的列向量,我想在原来的阵列中的各个点插入另一列向量。即我想我的新阵列添加到旧数组的开始跳过10个地方再次添加我的新阵,又跳过10个空格,再等我添加新的数组,直到数组末尾。我可以通过这样做:

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 start of the old array skip 10 places add my new array again, skip another 10 spaces and add my new array again and so on till the end of the array. I can do this by using:

OffsetSign = [1:30]';
Extra = [0;0;0;0;0];
OffsetSign =[Extra;OffsetSign(1:10);Extra;OffsetSign(11:20);Extra;OffsetSign(21:30)];

然而,这不适合于较长的阵列。在一个简单的方法任何提示,以更长的数组做到这一点?

However this is not suitable for longer arrays. Any tips on an easy way to do this for longer arrays?

推荐答案

下面是做到这一点的一种方法:

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(:);

关键是要重塑 A 用大小合适的列的矩阵(每次10元)。复制 B 来此#栏目,既串联和扁平化矩阵为矢量...

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...

这篇关于插入新的值到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆