将新值插入数组 [英] Insert new values into an array

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

问题描述

我目前有一个不同长度的列向量,我想在原始数组的各个点插入另一个列向量.即我想将我的新数组添加到旧数组的开头跳过 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?

推荐答案

这是一种方法:

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天全站免登陆