Simple for loop index

조회 수: 10 (최근 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