【发布时间】:2016-09-08 20:11:57
【问题描述】:
如何在数组 (a2) 中的每个 (a1)
中的每个 nth 位置插入元素示例:逻辑
a1 = [1,10,2,20,3,30,4,40,5,50];
a2 = [100,200,300,400,500];
n=3 % n would be the position to place the elements found in (a2) every **nth** position in (a1).
*n is the starting position at which the array a2 is inserted into a1*
插入 a2 后的 new a1 if n=3 看起来像
a1 = [1,10,100,2,20,200,3,30,300,4,40,400,5,50,500];
插入 a2 后的 new a1 if n=2 看起来像
a1 = [1,100,10,2,200,20,3,300,30,4,400,40,5,500,50];
new a1 if n=1 在插入 a2 后看起来像
a1 = [100,1,10,200,2,20,300,3,30,400,4,40,500,5,50];
我试过了
a1(1:3:end,:) = a2;
但我收到尺寸不匹配错误。
请注意,这只是一个示例,因此我不能只计算答案我需要将数据插入到数组中。 n 是数组 a2 插入 a1
的起始位置【问题讨论】:
-
从您的示例中,您似乎没有在每个
nth位置插入a2的元素,而是在每个3rd位置插入它们,从位置n开始.这就是你想要达到的目标吗? -
@beaker 是的,在这种情况下你是对的
-
"in this instance" 暗示其他实例的行为不同。除非您告诉我们在所有情况下应该采取什么行为,否则我认为没有人可以帮助您。