Find/Match index of variable from an array

조회 수: 205 (최근 30일)
Chris Dan
Chris Dan 2020년 2월 26일
댓글: Chris Dan 2020년 2월 26일
Hi, I have this code which I am using to find index of variable a from an array b
a = 2;
b =[ 1 2 3 4 5 6 7 8 9 10];
for i = 1:1:size(b,2)
if (find (a == b))
[~,zq(i)] = (min(b(i)- a));
end
end
The problem is the result "zq", in which I am getting all ones, so I donot know the position, where a is being matched.
Does any one know?
  댓글 수: 2
Alex Mcaulley
Alex Mcaulley 2020년 2월 26일
You are obtaining all ones because "b(i)- a" is scalar and the minimum value of a scalar is allways itself.
madhan ravi
madhan ravi 2020년 2월 26일
Second Alex.

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

채택된 답변

Bhaskar R
Bhaskar R 2020년 2월 26일
zq = find(b == a)
  댓글 수: 2
madhan ravi
madhan ravi 2020년 2월 26일
+1
Chris Dan
Chris Dan 2020년 2월 26일
Thanks (Y)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by