MATLAB Answers

0

How to find the position of an element in a vector

Asked by Cantor Set on 6 Sep 2019
Latest activity Answered by darova
on 6 Sep 2019
Hello,
Is there a function in matlab that takes a column vector of n rows and a number as an input and returns its position in the vector,
for example a vector x as given below, suppose I want to find the position of number 2 in x. I want the answer to be in the form of [ 1 4]
x=[2 3 4 2];
I thought of the following code
x=[2 3 4 2];
for i=1:4
if x(i)==2;
pos(i)=i;
i=i+1;
end
end
But the output is of the form
[1 0 0 4]

  0 Comments

Sign in to comment.

2 Answers

Answer by madhan ravi
on 6 Sep 2019
 Accepted Answer

find(x==2)

  1 Comment

Thank you so much

Sign in to comment.


Answer by darova
on 6 Sep 2019

Use find()

  0 Comments

Sign in to comment.