How to find the position of an element in a vector

45 ビュー (過去 30 日間)
Cantor Set
Cantor Set 2019 年 9 月 6 日
回答済み: darova 2019 年 9 月 6 日
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]

採用された回答

madhan ravi
madhan ravi 2019 年 9 月 6 日
  1 件のコメント
Cantor Set
Cantor Set 2019 年 9 月 6 日
Thank you so much

サインインしてコメントする。

その他の回答 (1 件)

darova
darova 2019 年 9 月 6 日
Use find()

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by