此代码用于剪切向量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);
答案 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);