Question about storing indices and using other functions at those indices

1 次查看(过去 30 天)
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!

采纳的回答

Star Strider
Star Strider 2015-3-21
编辑:Star Strider 2015-3-21
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;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by