HISTCOUNTS a true replacement of HISTC?
Mostra commenti meno recenti
Since few latest releases we get the code warning "histc is not recommended. Use histcounts instead".
However how to replace HISTC with specified DIM argument, for example what is the command to get C1 and C2 in this example?
I hope you won't tell me I need a for-loop or some sort disguised loop.
>> A=reshape(linspace(0,1,20),[4 5])
A =
0 0.2105 0.4211 0.6316 0.8421
0.0526 0.2632 0.4737 0.6842 0.8947
0.1053 0.3158 0.5263 0.7368 0.9474
0.1579 0.3684 0.5789 0.7895 1.0000
>> b=linspace(0,1,3)
b =
0 0.5000 1.0000
>> c1=histc(A,b,1)
c1 =
4 4 2 0 0
0 0 2 4 3
0 0 0 0 1
>> c2=histc(A,b,2)
c2 =
3 2 0
3 2 0
2 3 0
2 2 1
>>
Risposta accettata
Più risposte (3)
Bruno Luong
il 10 Ott 2018
Modificato: Bruno Luong
il 10 Ott 2018
1 Commento
Stephen23
il 10 Ott 2018
Nice. This definitely wins the Answer of the Day award.
Perhaps the next step would be to make an enhancement request.
OCDER
il 10 Ott 2018
Interesting finding that histcounts doesn't have the Dim option. You should ask TMW to add that feature - they must have overlooked that.
"I hope you won't tell me I need a for-loop or some sort disguised loop."
But, there really is no need to avoid a simpler for-loop solution. Also, your answer has a "disguised loop" inside the arrayfun - arrayfun uses an internal for loop.
A =[ 0 0.2105 0.4211 0.6316 0.8421;
0.0526 0.2632 0.4737 0.6842 0.8947;
0.1053 0.3158 0.5263 0.7368 0.9474;
0.1579 0.3684 0.5789 0.7895 1.0000];
Bin = linspace(0, 1, 3);
C1 = histc(A, Bin, 1);
C2 = myHistC(A, Bin, 1);
D1 = histc(A, Bin, 2);
D2 = myHistC(A, Bin, 2);
assert(isequal(C1, C2) && isequal(D1, D2), 'Not the same thing');
function C = myHistC(A, Bin, Dim)
if nargin < 3; Dim = 1; end
if Dim == 1
C = zeros(numel(Bin), size(A, 2));
for j = 1:size(A, 2)
C(:, j) = histcounts(A(:, j), [Bin(:); Inf]);
end
else
C = zeros(size(A, 1), numel(Bin));
for j = 1:size(A, 1)
C(j, :) = histcounts(A(j, :), [Bin(:); Inf]);
end
end
end
5 Commenti
Bruno Luong
il 10 Ott 2018
OCDER
il 10 Ott 2018
Oh, I see. Yeah, TMW should step in and suggest a better solution (if any). Plus, my solution also cannot handle the variable outputs generated by histc/histcounts. I'm curious as to what's the alternative to histc.
Guillaume
il 10 Ott 2018
"they must have overlooked that"
I'm not speaking for Mathworks, but I'd say they're very much aware of it. You just have to look at Replace Discouraged Instances of hist and histc which clearly states under Code Updates for histc:
"histcounts treats the input matrix as a single tall vector and calculates the bin counts for the entire matrix [...] Use a for-loop to calculate bin counts over each column. [...] If performance is a problem due to a large number of columns in the matrix, then consider continuing to use histc for the column-wise bin counts."
OCDER
il 10 Ott 2018
I see. But if the suggestion is to use histc, I'm wondering why histcounts doesn't implement this directly using a name-value input like
histcounts(..., 'dim', N) ?
Overall, I find it confusing that they acknowledged the shortcoming of histcounts, offered a workaround code in the example but do not implement this within histcounts, and then, made histc generate warning messages to use histcounts instead, which has no replacement for histc's column/row-wise binning. It's an interesting decision.
From the website:
Use a for-loop to calculate bin counts over each column.
A = randn(100,10);
nbins = 10;
N = zeros(nbins, size(A,2));
for k = 1:size(A,2)
N(:,k) = histcounts(A(:,k),nbins);
end
Bruno Luong
il 10 Ott 2018
Modificato: Bruno Luong
il 10 Ott 2018
Bruno Luong
il 10 Ott 2018
Modificato: Bruno Luong
il 10 Ott 2018
Categorie
Scopri di più su Signal Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!