No jump in in if-query even though if-statement is true

조회 수: 1 (최근 30일)
Stefanie Schwarz
Stefanie Schwarz 2019년 11월 12일
댓글: Stefanie Schwarz 2019년 11월 15일
In the following Code I want to compare the values of two different variables.
X = 0:0.1:200;
T = [0.1; 0.2; 0.3; 0.5; 0.9; 1.5; 2.6; 4.5; 7.7; 13.2; 22.8; 39.2; 67.5; 116.2; 200];
jump_in = zeros([length(T) 1]);
for t = 1:length(T)
for x = 1:length(X)
if X(x) == T(t)
jump_in(t) = 1;
break;
end
end
end
After running there are no warnings or errors but for the values of 0.3, 13.2 and 116.2 even if the statement is true it does not jump in.
I changed the if-statement into X(x) -T(t) == 0 but this did not work either.
I also changed the values of the Variables X and T into
X = [0.3:0.1:200];
T = [0.3; 0.5; 0.9; 1.5; 2.6; 4.5; 7.7; 13.2; 22.8; 39.2; 67.5; 116.2; 200];
Then it jumps into the if-query for 0.3 but it does not jump in for the vaules of 0.9, 1.5, 13.2 and 116.2.
Does someone has expierence with this problem or a hint?
My Matlab version is R2019a and I did not used any toolboxes or blocksets.
Thank you.
  댓글 수: 2
Stefanie Schwarz
Stefanie Schwarz 2019년 11월 15일
Thank you, for posting these links. I will read them carefully.

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

채택된 답변

Rik
Rik 2019년 11월 12일
Not all decimal numbers can be stored in binary as an exact value. This float rounding error can lead to situations like this. You can solve this by testing against a tolerance (functions like ismembertol and uniquetol are also useful).
If abs(X(x) -T(t)) <= eps
  댓글 수: 2
Stefanie Schwarz
Stefanie Schwarz 2019년 11월 12일
I checked your answers and it works fine for ismembertol() and uniquetol().
With the
If abs(X(x)-T(t)) <= eps
it jumps in for 0.3 but not for 13.2 and 116.2.
Thank you
Rik
Rik 2019년 11월 12일
When using ismembertol, don't forget to set 'DataScale',1 because otherwise you might get unexpected results. Refer to the documentation to see if you want to leave the scaling on auto, or if you want to set it to 1.
As a side-note: I personally tend to use 2*eps, although there shouldn't really be a difference.
Glad to be of help.

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

추가 답변 (1개)

Bhaskar R
Bhaskar R 2019년 11월 12일
I don't know,why are you taking variable jump_in.
From your code we can summarize as
% c- common values(X(x) == T(t))
% iX - indices if vector X
% iT - indices if vector T
[c, iX, iT] = intersect(X,T);
jump_in = ones(1, length(iX));
Correct me if I am wrong
  댓글 수: 1
Stefanie Schwarz
Stefanie Schwarz 2019년 11월 12일
By reducing my code to the actuall problem I turned the variable X from a 3x2001 size to a 1x2001. This is why jump_in seems useless but in my full code I wanted to also save the data of the other two rows of X in jump_in. So jump_in has an actuall size of 3xlength(T). Thank you for telling me about intersect(), it will come in handy.

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

카테고리

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