Simple for loop index

12 次查看(过去 30 天)
Dirk
Dirk 2015-12-10
评论: Torsten 2015-12-11
Hi,
Here is a simple piece of code to find the location of elements n within the vector A
A=[2,4,6,7,9];
for n=[4,9];
x=find(n==A);
end
However x only gives me the last location, i.e. 5, whereas I want the whole list of locations, i.e. [2,9] Thanks

采纳的回答

Torsten
Torsten 2015-12-10
A=[2,4,6,7,9];
index=1;
for n=[4,9]
x(index)=find(n==A);
index=index+1;
end
Best wishes
Torsten.
  3 个评论
Dirk
Dirk 2015-12-10
Why does this not work with when a decimal is present (for example in A)?
A=[2,4.4,6,7,9];
index=1;
for n=[4,9]
x(index)=find(n==A);
index=index+1;
y=A(x);
end
Torsten
Torsten 2015-12-11
A=[2,4.4,6,7,9];
index=1;
for n=[4,9]
k=find(n==A);
x{index}=k;
y{index}=A(k);
index=index+1;
end
Best wishes
Torsten.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by