1

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], 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); 
7
  • @rayryeng, Which part is not clear? Commented Feb 16, 2016 at 6:27
  • Your code seems to run. I don't understand what it is you want and please forgive me if I am rude, but the language in your question is a bit hard to understand. Commented Feb 16, 2016 at 6:28
  • 1
    What is the initial vector? Commented Feb 16, 2016 at 6:29
  • @R.Bergamote, This : ch = [4; 5; 6; 7; 8; 9]; Commented Feb 16, 2016 at 6:31
  • 1
    @shdotcom what is wrong with this code? What is the output you are getting and what is the output that you are expecting? Share these both for the community to understand what you are trying to ask. Commented Feb 16, 2016 at 6:35

1 Answer 1

2

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); 
Sign up to request clarification or add additional context in comments.

2 Comments

when I run the code I got this output: a= 5 6 4 ch = 7 8 5 6 4 8 9 The second "8" should not be there, the output should be: ch = 7 8 5 6 4 9
I have ganged this: ch(startIdx:end)]; to: ch(startIdx+1:end)]; and its working now. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.