how to find the location of a specific array element within the specific range in matlab?

조회 수: 1 (최근 30일)
for example: d=[1 2 3 4 8 10 4 5 6 12 10 13];
i want to find the location of 10 within the range [4 8]
the expected answer would be 6.
I used the following command: a=find(d(4:8)==10);
the answer i am getting is 3 instead of 6.
Plz suggest me a way to solve this.

답변 (2개)

José-Luis
José-Luis 2016년 12월 20일
편집: José-Luis 2016년 12월 21일
EDITED to avoid the use of range
d=[1 2 3 4 8 10 4 5 6 12 10 13];
idx(numel(d)) = 0;
idx(4:8) = 1;
a = find(d == 10 & idx)
  댓글 수: 2
Image Analyst
Image Analyst 2016년 12월 20일
If you have the stats toolbox, you might want to rename since range is a built-in function of that toolbox.
José-Luis
José-Luis 2016년 12월 21일
편집: José-Luis 2016년 12월 21일
Thanks, good point. While range should have rung a bell, there are so many reserved names that I wonder if this is headed towards the atrocity that dunders are.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 12월 20일
Here's a way:
d=[1 2 3 4 8 10 4 5 6 12 10 13];
startingIndex = 4;
endingIndex = 8;
valueToLookFor = 10;
locations = find(d(startingIndex : endingIndex) == valueToLookFor) + startingIndex - 1

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by