Borrar filtros
Borrar filtros

Question about storing indices and using other functions at those indices

1 visualización (últimos 30 días)
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!

Respuesta aceptada

Star Strider
Star Strider el 21 de Mzo. de 2015
Editada: Star Strider el 21 de Mzo. de 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;

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by