Azzera filtri
Azzera filtri

Question about storing indices and using other functions at those indices

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

Star Strider
Star Strider il 21 Mar 2015
Modificato: Star Strider il 21 Mar 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;

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by