简体   繁体   中英

Insert vector into vector, Matlab

This code is to cut random values from the vector ch and create new vector a . Then, insert a into ch after delete a selected values from ch

What I should change, to be the result like this:

for example if a = [8; 4; 9] a = [8; 4; 9] a = [8; 4; 9] , then the result :

ch = 5 8 4 9 6 7

Matlab code:

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

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM