less than or equal and greater than of equal operations

조회 수: 5 (최근 30일)
ektor
ektor 2018년 8월 4일
편집: jonas 2018년 8월 4일
Dear all,
I have this vector
DIFF=[ 0
0.0500
-0.0200
0.0100
-0.0400
0
0
0
0.0500
0
0.0200
0
0.0100
0
-0.0100
0
-0.1500
0
-0.0100
0];
and I want to make the following tranformation:
The values of DIFF<-0.02 to be tranformed to 1
The values of DIFF>=-0.02 & DIFF<-0.01 to be tranformed to 2
The values of DIFF>=-0.01 & DIFF<=0 to be tranformed to 3
The values of DIFF>0 & DIFF<=0.01 to be tranformed to 4
The values of DIFF>0.01 & DIFF<=0.02 to be tranformed to 5
The values of DIFF>0.02 to be tranformed to 6
So I constructed the following code
c1= -0.02;
c2= -0.01;
c3= 0;
c4= 0.01;
c5= 0.02;
kk=size(DIFF,1);
vy=zeros(kk,1);
f1=find( DIFF<c1);
f2=find(DIFF>=c1 & DIFF<c2);
f3=find(DIFF>=c2 & DIFF<=c3);
f4=find(DIFF>c3 & DIFF<=c4);
f5=find(DIFF>c4 & DIFF<=c5);
f6=find(DIFF>c5);
vy(f1)=1;
vy(f2)=2;
vy(f3)=3;
vy(f4)=4;
vy(f5)=5;
vy(f6)=6;
And I finally obtain the following matrix
DATA_NEW=[vy DIFF]
3.0000 0
6.0000 0.0500
1.0000 -0.0200
4.0000 0.0100
1.0000 -0.0400
3.0000 0
3.0000 0
3.0000 0
6.0000 0.0500
3.0000 0
5.0000 0.0200
3.0000 0
4.0000 0.0100
3.0000 0
3.0000 -0.0100
3.0000 0
1.0000 -0.1500
3.0000 0
3.0000 -0.0100
3.0000 0
But as you can see from the third element of DATA_NEW, the value of 1 is assigned to -0.02. But according to my categorization, the value of 2 should have been assigned to -0.02.
Similarly, the value of 6 is assigned to 0.05, but it should be the value of 5.
How could I rectify this problem given the fact that my real DIFF vector contains thousands of elements.
Thanks in advance

채택된 답변

Star Strider
Star Strider 2018년 8월 4일
The vector you posted may not be showing the full precision of the values in it. If you calculated them (or imported them without looking at them), they may not be what they seem. See Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link) for an explanation.
  댓글 수: 2
ektor
ektor 2018년 8월 4일
편집: ektor 2018년 8월 4일
the displayed vector is of full precision. I also used
format shortG
I use Matlab R2017a.
Star Strider
Star Strider 2018년 8월 4일
With the data you posted, I got (in R2018a):
DATA_NEW =
3 0
6 0.05
2 -0.02
4 0.01
1 -0.04
3 0
3 0
3 0
6 0.05
3 0
5 0.02
3 0
4 0.01
3 0
3 -0.01
3 0
1 -0.15
3 0
3 -0.01
3 0
If you use:
format long g
you will likely see the problem.

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

추가 답변 (1개)

jonas
jonas 2018년 8월 4일
편집: jonas 2018년 8월 4일
There is no problem with your code. In fact, if you copy your code as written in the question, you get the following results.
ans
3.0000 0
6.0000 0.0500
2.0000 -0.0200
4.0000 0.0100
1.0000 -0.0400
3.0000 0
3.0000 0
3.0000 0
6.0000 0.0500
3.0000 0
5.0000 0.0200
3.0000 0
4.0000 0.0100
3.0000 0
3.0000 -0.0100
3.0000 0
1.0000 -0.1500
3.0000 0
3.0000 -0.0100
3.0000 0
I am guessing that the value displayed as -0.0200 is actually less than -0.02.
Try writing
DIFF(3)-0.02
The answer will be less than zero, which implies that DIFF(3) is less than -0.02.
As a sidenote, you can substitute the find command with logical indexing. Instead of
f1=find(DIFF<c1);
vy(f1)=1;
you can write
vy(DIFF<c1)=1
  댓글 수: 2
ektor
ektor 2018년 8월 4일
편집: ektor 2018년 8월 4일
I set
DIFF(3)==-0.02
but again the same problem. you can try yourself to verify this.
I use Matlab R2017a
jonas
jonas 2018년 8월 4일
편집: jonas 2018년 8월 4일
What do you mean, again the same problem? I am quite sure that
DIFF(3)==-0.02
returns 0 in your case, because the number displayed as -0.02 is actuall less than -0.02 (e.g. -0.02000000000001), otherwise you would not be asking this question. It is not a problem, the code works as intended.
I can not verify this, because the numbers you posted are not the same as the ones you have in your workspace. Instead, you can try to clear your workspace and replace it with the code you gave us in your question. I am confident that you will get the (according to you) correct result.
Note that you do not SET anything when you use two equal signs, you TEST whether DIFF(3) is equal to -0.02. Change it to single equal sign and you will get the "correct" results.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by