필터 지우기
필터 지우기

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