필터 지우기
필터 지우기

Please help me understand output from discretize

조회 수: 37 (최근 30일)
Jeff Spector
Jeff Spector 2024년 7월 29일 17:25
댓글: Walter Roberson 2024년 7월 29일 19:30
I am trying to understand what discretize is doing, I have data ranging from 0 to 1 and want to bin it in bins of size .01.
X=[.01:.01:1] ; % - example data uniformly going form .01 to 1 in steps of .01
binEdges=[0:0.01:1]; %- the bind edges
binLocs=discretize(X,binEdges,'IncludedEdge','right'); %
%I would expect each bin gets 1 value so that bon locs is [1 2 3 4 5 6 7 8 9 10 etc... up to 100]
but what I get is
binLocs=
1 2 3 4 5 7 7 8 9 10 11 12 13 14 16 16 17 19 19 20 22 22 23 25 25 26 27 28 30 30 31 32 33 34 35 37 37 38 39 40 41 43 43 44 45 46 47 49 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
why is is repeating some (for example the values .06 and .07 go in bin 7 and nothing in bin 6 ?
I'd like the first bin to b 0<=x<=.01 bin 2 .01<x<=.02 bin 3 .02<x<=.03 etc..
can someone explain why I am getting 2 values in some bins ( i.e. bin 7 and bin 22)
Thanks,
-Jeff

채택된 답변

Torsten
Torsten 2024년 7월 29일 17:57
편집: Torsten 2024년 7월 29일 18:13
X=[.01:.01:1] ; % - example data uniformly going form .01 to 1 in steps of .01
binEdges=[0:0.01:1]; %- the bind edges
X(6)>binEdges(7) & X(6)<=binEdges(8)
ans = logical
1
X(7)>binEdges(7) & X(7)<=binEdges(8)
ans = logical
1
Since your data are on the edges of the bins, it's a precision problem in several cases.
  댓글 수: 2
Jeff Spector
Jeff Spector 2024년 7월 29일 18:28
편집: Jeff Spector 2024년 7월 29일 18:29
I thought that 'includge edge, right" would take of that. I still don't understand the following :
X(6)
ans =
0.0600
binEdges(7)
ans =
0.0600
.0600>06000
ans =
logical
0
%- this makes sense since they are equal but then
.0600>binEdges(7)
ans =
logical
0
%- ok this is same as above, now replace .0600 with X(6)
X(6)
ans =
0.0600
>> X(6)>binEdges(7)
ans =
logical
1
%- I guess I don't understand why this is happening? Can someone enlighten me. It seems to get it correct if I'm not using "X(6) but rather the value of X(6). This seems insane to me so please help me understand what I am missing.
Thanks,
-Jeff
Torsten
Torsten 2024년 7월 29일 18:41
편집: Torsten 2024년 7월 29일 18:45
X(6) is not 0.0600. It's only printed as output with this limited length.
X=[.01:.01:1] ; % - example data uniformly going form .01 to 1 in steps of .01
binEdges=[0:0.01:1]; %- the bind edges
binEdges(7)-X(6)
ans = -6.9389e-18
X(6)-binEdges(8)
ans = -0.0100
binEdges(7)-X(7)
ans = -0.0100
X(7)-binEdges(8)
ans = -1.3878e-17

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2024년 7월 29일 18:56
X=[.01:.01:1] ; % - example data uniformly going form .01 to 1 in steps of .01
binEdges=[0:0.01:1]; %- the bind edges
X - round(X * 100)/100
ans = 1x100
1.0e-15 * 0 0 0 0 0 0.0069 -0.0139 0 0 -0.0139 0 0 0 0 0.0278 0 0 0.0278 0 0 0.0278 0 0 0.0278 0 0 0 0 0.0555 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
binEdges - round(binEdges*100)/100
ans = 1x101
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So, starting from 0.01 and incrementing by 0.01 does not lead to edges that are exactly what you would naively predict.
  댓글 수: 2
Jeff Spector
Jeff Spector 2024년 7월 29일 19:13
Thanks. I sitll don't really understand where the number are coming from ( i.e. why aren't they .01,.02 etc.. but just using round (X,5) seems to fix the issue, and I can easily tolerate that level of roundig so thanks so much!
-Jeff
Walter Roberson
Walter Roberson 2024년 7월 29일 19:30
X = 0.01:0.01:1;
Y = (1:100)/100;
X(6:7) - Y(6:7)
ans = 1x2
1.0e-16 * 0.0694 -0.1388
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fprintf('X(6:7) = %08x %08x\n', typecast(X(6:7), 'uint64'));
X(6:7) = 3faeb851eb851eb9 3fb1eb851eb851eb
fprintf('Y(6:7) = %08x %08x\n', typecast(Y(6:7), 'uint64'));
Y(6:7) = 3faeb851eb851eb8 3fb1eb851eb851ec
fprintf('X5+ = %08x\n', typecast(X(5) + 0.01, 'uint64'))
X5+ = 3faeb851eb851eb9
I would guess that 0:0.01:1 is being treated internally as (0:100)/100 whereas 0.01:0.01:1 is treated as repeated addition, and repeated addition suffers from round-off error.

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

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by