필터 지우기
필터 지우기

Accessing a value in a vector from a conditional statement

조회 수: 2 (최근 30일)
Frank Lehmann
Frank Lehmann 2020년 7월 5일
댓글: Frank Lehmann 2020년 7월 5일
Hi All,
Im trying to access a vlue in the middle of my vector as per below. From my mv value of 13.073 i should receive the value of 25 from the vector, ie next highest from mv. I would like to get both the value and index
v=[6 10 25 35 50 65 80]
x=10.02
y=10.47
mv=max(x*.125,y*1.25)
for k=1:length(v)
if v(k)>mv
mv=v(k)
ind=k
end
end
Thanks,
Everyone

답변 (2개)

Star Strider
Star Strider 2020년 7월 5일
There are several ways to do this.
My approach:
v=[6 10 25 35 50 65 80];
x=10.02;
y=10.47;
mv=max(x*.125,y*1.25);
ind = interp1(v,(1:numel(v)), mv, 'next')
Out = v(ind)
producing:
ind =
3
Out =
25
I prefer not to overwrite existing variables, so I have the output as ‘Out’.
.

Image Analyst
Image Analyst 2020년 7월 5일
Use find():
v=[6 10 25 35 50 65 80]
x=10.02
y=10.47
mv=max(x*.125,y*1.25)
[~, index] = find(v >= mv, 1, 'first')
value = v(index)
index =
3
value =
25
  댓글 수: 1
Frank Lehmann
Frank Lehmann 2020년 7월 5일
Thanks guys much appreciated it also helps having a few different solutions to keep in mind next time.

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by