How could I write in a vector something in a position different to a given one?

1 view (last 30 days)
v = [1,2,3,4,5]
idx = 3;
What I would like to do is get v = [NaN, NaN, 3, NaN, NaN];
How could I do it?

Accepted Answer

KSSV
KSSV on 15 Oct 2021
v = [1,2,3,4,5];
idx = 3;
iwant = NaN(size(v)) ;
iwant(idx) = v(idx)
  1 Comment
Juan Manuel Hussein Belda
Juan Manuel Hussein Belda on 15 Oct 2021
Thank you so much for the reply!! However, I think I tried to simplify the question and ended up asking it wrong. I will ask again, and sorry for the inconvenience!!
I have two vectors, see:
result = [7,5,6,4,0];
v_sample = [1,3,4,0,0];
v_real = [1,2,3,4,5];
What I would like is to obtain the following:
v_sample_new = [1, NaN, 3, 4, Nan]
result_new = [7, NaN, 5, 6, NaN]
So, basically, v_sample_new is ordered with respect to v_real, and result has the correspondant values ( result(i) is related to v_sample(i) ) in a manner that is consistent as shown. I do noy know if I have explained it properly but I hope so, and thank you so much again!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!