x=rand(1,10); bins=discretize(x,0:0.25:1);
An instance of running the above line in Matlab R2020b produces the following outputs for x and bins.
x = 0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595
bins = 1, 4, 4, 2, 4, 1, 2, 4, 4, 4
The in-built function discretize is not yet implemented in Octave. How can I achieve the same values of bins in OCTAVE? Can anyone enlighten me? I am using Octave 6.2.0.
CodePudding user response:
tl;dr:
x = [ 0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595 ]
[ ~, Bins ] = histc( x, 0: 0.25: 1 )
% Bins = 1 4 4 2 4 1 2 4 4 4
Explanation:
According to the matlab manual:
Earlier versions of MATLAB® use the
hist
andhistc
functions as the primary way to create histograms and calculate histogram bin counts [...] The use of hist and histc in new code is discouraged [...]histogram
,histcounts
, anddiscretize
are the recommended histogram creation and computation functions for new code.
and
The behavior of discretize is similar to that of the histcounts function. Use histcounts to find the number of elements in each bin. On the other hand, use discretize to find which bin each element belongs to (without counting).
Octave has not yet implemented discretize
, but still supports histc
, which as the above implies, does the same thing but with a different interface.
According to the octave documentation of histc
-- [N, IDX] = histc ( X, EDGES )
Compute histogram counts.
[...]
When a second output argument is requested an index matrix is also
returned. The IDX matrix has the same size as X. Each element of
IDX contains the index of the histogram bin in which the
corresponding element of X was counted.
Therefore the answer to your problem is
[ ~, Bins ] = histc( x, 0:0.25:1 )
Using your example:
x = [ 0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595 ]
[ ~, Bins ] = histc( x, 0: 0.25: 1 )
% Bins = 1 4 4 2 4 1 2 4 4 4
PS. If you like the interface provided by discretize
, you can easily create this function by yourself, by wrapping histc
appropriately:
discretize = @(X, EDGES) nthargout( 2, @histc, X, EDGES )
You can now use this discretize
function directly as in your example.
CodePudding user response:
You can use interp1 with 'previous' option:
edges = 0:0.25:1;
x = [0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595];
bins = interp1 (edges, 1:numel(edges), x, 'previous')
Another function that can be used is lookup
:
bins = lookup(edges, x);
Here I compared the performance of interp1
, lookup
and also histc
as recommended by Tasos Papastylianou:
edges = 0:0.025:1;
x = sort (rand(1,1000000));
disp ("-----INTERP1-------")
tic;bins = interp1 (edges, 1:numel(edges), x, 'previous');toc
disp ("-----HISTC-------")
tic;[ ~, bins ] = histc (x, edges);toc
disp ("-----LOOKUP-------")
tic; bins = lookup (edges, x);toc
The result:
-----INTERP1-------
Elapsed time is 0.0593688 seconds.
-----HISTC-------
Elapsed time is 0.0224149 seconds.
-----LOOKUP-------
Elapsed time is 0.0114679 seconds.