Checking a specific element of 4x1 matrix being 1

조회 수: 1 (최근 30일)
Das Siddharth
Das Siddharth 2021년 4월 25일
댓글: Das Siddharth 2021년 4월 26일
I am trying this very simple code in which I want to check a particular element of this 4x1 matrix whether it is 1 or not. For some reason the entire code is skipped and not even showing anything.
for i=1:4
if probFinal(i,1) == 1
disp('Equal');
end
end
Some values of probFinal are [1.00000000000000 ; 0 ; 5.00468046766525e-34 ;0] && [0 ; 1.00000000000000 ; 0 ; 5.00468046766525e-34] and all the permutation including these values only.

채택된 답변

DGM
DGM 2021년 4월 25일
편집: DGM 2021년 4월 25일
More than likely, this is a case of rounding error. Beware strict equality tests with floating point numbers. That 1.000000000 isn't 1. It's approximately 1. You should consider testing against a tolerance:
tol=1E-12
for i=1:4
if abs(probFinal(i,1)-1)<=tol
% do things
end
end
  댓글 수: 1
Das Siddharth
Das Siddharth 2021년 4월 26일
Thank you so much DGM. I didn't realize this could be it. I thought MATLAB would understand anyway, sigh. I thank you again.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by