Please help me understand output from discretize
8 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Torsten
on 29 Jul 2024
Edited: Torsten
on 29 Jul 2024
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)
X(7)>binEdges(7) & X(7)<=binEdges(8)
Since your data are on the edges of the bins, it's a precision problem in several cases.
2 Comments
Torsten
on 29 Jul 2024
Edited: Torsten
on 29 Jul 2024
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)
X(6)-binEdges(8)
binEdges(7)-X(7)
X(7)-binEdges(8)
More Answers (1)
Walter Roberson
on 29 Jul 2024
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
binEdges - round(binEdges*100)/100
So, starting from 0.01 and incrementing by 0.01 does not lead to edges that are exactly what you would naively predict.
2 Comments
Walter Roberson
on 29 Jul 2024
X = 0.01:0.01:1;
Y = (1:100)/100;
X(6:7) - Y(6:7)
fprintf('X(6:7) = %08x %08x\n', typecast(X(6:7), 'uint64'));
fprintf('Y(6:7) = %08x %08x\n', typecast(Y(6:7), 'uint64'));
fprintf('X5+ = %08x\n', typecast(X(5) + 0.01, 'uint64'))
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.
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!