Accumarray error for logspace binning
3 views (last 30 days)
Show older comments
Hi all,
I'm trying to bin some of my data according to a nonuniform spacing method based on a logarithmic scale, whereby I create 100 bins from the minimum value to the maximum value. First I use histc function to generate bin indices, then I try using accumarray to build the matrix based on the mean value as follows:
[unused, binidx] = histc(M4M2_2, ...
logspace(log10(min( M4M2_2 )), log10(max( M4M2_2) ),100+1));
binidx( isnan( M4M2_2 ) ) = [];
binidx = uint8( binidx );
M4M2_2n = M4M2_2; M4M2_2n( isnan( M4M2_2n ) ) = [];
means = accumarray(binidx, M4M2_2n, [], @mean);
However, this does not work even after trying to make the binidx variable into an integer variable using uint8. I still get the following error, which I cannot figure out because I checked that all the values in the binidx matrix were in fact positive integers:
Error using accumarray
First input SUBS must contain positive integer subscripts.
2 Comments
Matt J
on 27 May 2016
I checked that all the values in the binidx matrix were in fact positive integers:
Attach a .mat file containing binidx and M4M2_2n so we can convince ourselves of that.
Answers (3)
Star Strider
on 27 May 2016
I can’t follow what you are doing. If you want to plot binned data logarithmically, this code from an Answer I provided a couple years ago may do what you want:
x = 10+2*randn(1, 100); % Create data
bins = linspace(floor(min(x)), ceil(max(x)), 25); % Define bins
xc = histc(x,bins); % Do histogram counts
figure(1)
bar(log(bins), xc) % Plot bars against log of ‘bins’
logxts = get(gca, 'XTick'); % 'XTick' values currently logarithmic
expxts = exp(logxts); % Take antilog to use them as new ‘XTickLabels’
set(gca, 'XTickLabel', floor(100*expxts)/100)
You will probably have to experiment with it to get the result you want.
0 Comments
Roger Stafford
on 27 May 2016
If the line
M4M2_2n = M4M2_2; M4M2_2n( isnan( M4M2_2n ) ) = [];
succeeds in shortening M4M2_2, then the required “one-to-one” matching between binidx and M4M2_2n is violated. Conceivably this could cause a misleading error message such as you received. Here is a quote from the ‘accumarray’ site:
http://www.mathworks.com/help/matlab/ref/accumarray.html
“In both cases, a one-to-one pairing is present between the subscripts in each row of subs and the data values in val.” In any case, it is a mismatch that you surely would want to correct.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!