Error in splitapply command

6 次查看(过去 30 天)
Ivan Mich
Ivan Mich 2021-2-27
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?

回答(1 个)

Image Analyst
Image Analyst 2021-2-27
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 个评论
Ivan Mich
Ivan Mich 2021-7-27
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 2021-7-27
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by