Error in splitapply command

9 views (last 30 days)
Ivan Mich
Ivan Mich on 27 Feb 2021
Commented: Image Analyst on 27 Jul 2021
I am using this command "splitapply" in order to find mean (average) of a group of data.
edges=1:0.5:10
[N, edges, bin] = histcounts(B, edges);
mean_B=splitapply(@mean, B, bin) %mean
%B is 1000x1 double
But command window shows me :
Error using splitapply (line 61)
Group numbers must be a vector of positive integers, and cannot be a sparse vector.
which is curiousness because for an another set of data code runs.
Could you please help me?

Answers (1)

Image Analyst
Image Analyst on 27 Feb 2021
This seems to work fine:
B = 1 + 9 * rand(1, 100000);
edges = 1 : 0.5 : 10
[counts, edges, bin] = histcounts(B, edges);
% bin says what bin the value was placed into.
% Compute the means of the values in each bin.
mean_B = splitapply(@mean, B, bin)
Attach your B so we can see why it's different than mine. If your B exceeds 10, it will say that bin is zero for those values exceeding 10, and that would be a problem since you're passing in bin as the "group number" and the group numbers have to be natural numbers (1,2,3,4,...) and not zero.
  19 Comments
Ivan Mich
Ivan Mich on 27 Jul 2021
Look for example from the first set I will have 5 bins with 5 mean numbers. From the second set I will have 4 bins with 4 mean numbers. every mean number corresponds to one bin . I am giving you an example of the output
set 1
Bin mean
[2-3] 0.5
[3-4] 1.25
[4-5] 1.6
[5-6] 1.9
[6-7] 3.2
set 2
Bin mean
[2.5-3.5] 0.75
[3.5-4.5] 1
[4.5-5.5] 1.7
[5.5-6.5]2.5
So I mean merge to have an output that will includes all the values.
Like
mean
0.5
0.75
1
1.6
1.7
1.9
2.5
3.2
That's what I mean.
Could you please help me?
Image Analyst
Image Analyst on 27 Jul 2021
The two sets are using different edges for some reason. That's probably not good and you should specify the edges to be the same for all sets. What do you want the edges to be for the combined set?
But my answer would be that what you asked to do is, in my opinion, not good. You should just histogram your combined original data set and not have two histograms (one from each data set) that have different edges. Just histogram the whole combined set with one set of edges.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps 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!