Accumarray error for logspace binning

3 Ansichten (letzte 30 Tage)
Drew Mahedy
Drew Mahedy am 27 Mai 2016
Bearbeitet: Matt J am 27 Mai 2016
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 Kommentare
Matt J
Matt J am 27 Mai 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.
Drew Mahedy
Drew Mahedy am 27 Mai 2016
Try now

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Star Strider
Star Strider am 27 Mai 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.

Roger Stafford
Roger Stafford am 27 Mai 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.
  1 Kommentar
Drew Mahedy
Drew Mahedy am 27 Mai 2016
The fact the binidx line comes first means that binidx gets shortened based on M4M2_2 before M4M2_2 gets shortened based on itself.

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 27 Mai 2016
Bearbeitet: Matt J am 27 Mai 2016
Your binidx values are non-negative, but are not positive as you claim:
>> min(binidx)
ans =
0

Community Treasure Hunt

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

Start Hunting!

Translated by