Filter löschen
Filter löschen

Question about storing indices and using other functions at those indices

1 Ansicht (letzte 30 Tage)
This is a general question about matlab that I'm stuck on. Let's say you have a vector v with n elements. Whenever the an element in the vector equals a particular value, I want to remember which element it was and then set the values of another vector at that element number to some particular value. This should be evaluated at every element where the equality is true.
For example, let's say that the vector v is a column vector and particular elements such as the 1st, 10th and 25th all equal 5. I have a vector x that has the same dimensions. When I find these element numbers, I want to set the x vector at these element numbers to a different value, such as 0. The x vector is already populated prior to this happening. I'm not sure how to code this properly in MATLAB.
Thank you so much in advance!

Akzeptierte Antwort

Star Strider
Star Strider am 21 Mär. 2015
Bearbeitet: Star Strider am 21 Mär. 2015
The find function is your friend here!
Example:
v = randi(10, 25, 1);
x = randi(50, 25, 1);
v5 = find(v == 5);
x(v5) = 0;
You can also use ‘logical indexing’ to do this in one line:
x(v == 5) = 0;

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by