将向量插入向量,Matlab

时间:2016-02-16 06:13:09

标签: matlab

此代码用于剪切向量ch中的随机值并创建新向量a。然后,在从a

中删除所选值后,将ch插入ch

我应该改变什么,成为这样的结果:

例如,如果a = [8; 4; 9],则结果为:

ch = 5 8 4 9 6 7

Matlab代码:

ch = [4; 5; 6; 7; 8; 9];

for i = 1:3
    g = randi(3);
    a(i) =  ch(g);
    ch(g) = [];
end;

startIdx = 2;
finalIdx = startIdx + size(a,1) - 1; 
ch(startIdx:finalIdx) = a; 

disp (ch);

1 个答案:

答案 0 :(得分:2)

试试这个:

ch = [4; 5; 6; 7; 8; 9];

for i = 1:3
    g = randi(3);
    a(i) =  ch(g);
    ch(g) = [];
end;

a = a'; % your problem probably come from mixing column / lines

startIdx = 2;
ch = [ch(1:startIdx); a; ch(startIdx+1:end)]; 


disp (ch);