Replace elements in vector by keeping specific positions
Show older comments
Hello,
I have two vectors that I would like to plot together but with different colours :
vector_1 = [1 3 5 6 8 10]
vector_2 = [2 4 7 11]
I make a vector that combines these two and I sort it in ascending order because for me this matters (for when I will plot them)
vector_combined = [vector_1 vector_2];
sorted_vector_combined = sort(vector_combined);
then I want to plot one vector in one color and another one in another color and superimpose them to see which points of which vector come before/after, so I do:
tup = ismember(sorted_vector_combined, vector_1),
and I get
tup = [1 0 1 0 1 1 0 1 1 0]
Now I want to preserve this structure with 1 and 0 because when I plot them I want to superimpose vector_2 in the 0 of vector_1 so then I do:
for k= 1:length(sorted_vector_combined)
if tup(k) == 1;
tup(k)= sorted_vector_combined(k);
else tup(k)== 0
end
end
However --> I get this:
ans =
1
ans =
1
ans =
1
ans =
1
and tup is still: [1 0 1 0 1 1 0 1 1 0]
why doesn't my code replace the elements '1' of tup with elements of sorted_vector-combined in the same position?
Thank you!!
Accepted Answer
More Answers (0)
Categories
Find more on Line Plots 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!