Error in splitapply command
Mostra commenti meno recenti
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?
Risposte (1)
Image Analyst
il 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 Commenti
Walter Roberson
il 27 Feb 2021
2 0.73834617
Your bins start at 1 but you have data less than 1 which would be getting a bin number of 0
Ivan Mich
il 27 Feb 2021
Ivan Mich
il 27 Feb 2021
Image Analyst
il 27 Feb 2021
That's not the full code. For example there is no code to read in 'data.txt' and derive B from it. And you seem to have two input files: a text file and a workbook. Which is it? In general to get rid of values less than 1 you can do
badNumberMask = data < 1;
B = data(badNumberMask);
Ivan Mich
il 27 Feb 2021
Image Analyst
il 27 Feb 2021
The last line got cut off. We're not seeing how B is computed from A. Come on, make it EASY for us to help you, not hard. And did you try my code to remove numbers less than 1?
Ivan Mich
il 27 Feb 2021
Image Analyst
il 27 Feb 2021
Ivan:
Just give me the whole script. Attach it with the paper clip icon. Because the following code, with the data.txt you attached (and I'm attaching again here) doesn't work. It doesn't give me A or B.
fileName1 = 'data.txt';
[d1, tex] = importdata(fileName1)
A = d1.data(:,1);
% B is a result from an equation including A values.
% the equation is
B = 4 * A - 0.3
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)
I'm very close to giving up on this, but giving you another chance to make it right. I'm sure over the past 3 hours you have new code by now so attach that. I don't have much time today to go back and forth on this just to get to a starting point.
Dot indexing is not supported for variables of this type.
Error in test7 (line 4)
A = d1.data(:,1);
Walter Roberson
il 27 Feb 2021
Your first column has values up to 10. 4*10-0.3 is 39.7 and 39.7 is outside the range 1:0.5:10 so values anywhere near that large would get assigned bin 0.
Your second column has values up to about 7.5. 4*7.5-0.3 is about 29.7 and 29.7 is outside the range 1:0.5:10 so values anywhere near that large would get assigned 0.
Your first column has values from 2 to 10, and that would fit in 1:0.5:10 if you used it directly.
Your second column has values from 0.73834617 to about 7.5, but 0.738 is before 1:0.5:10 so those small values would be assigned bin 0 if you were to bin your second column directly.
Ivan Mich
il 28 Feb 2021
Walter Roberson
il 28 Feb 2021
mask = B < 1;
newA = A(mask) ;
newB = B(mask);
Image Analyst
il 28 Feb 2021
Keep in mind that you can't delete/remove elements from a array with more than 2 dimensions because it must remain rectangular. It can't have "holes" or "ragged edges" in it. You can only remove elements from a 1-D vector.
If you have a 2-D matrix and get a map of where it's above or below some threshold, then if you pass that into the 2-D matrix as a logical index, it can't give you a 2-D matrix back with "holes" in it where the non-selected elements are missing. So it returns the elements all concatenated in a 1-D vector pulled from the original matrix in a "column major" manner.
Ivan Mich
il 26 Lug 2021
Walter Roberson
il 27 Lug 2021
discretize() to get bin numbers. grpstats() to get the mean (if you have the Statistics toolbox; otherwise you can use splitapply() or accumarray() )
You can do the above for each of the two sets of edges.
However... I do am not clear on how you would want to merge the two results ?
I guess I am also not clear as to whether the data mentioned in (1) is the same data as is mentioned in (3) or if it is different data.
Are you trying to divide data with (X, Y, value) up across a grid and take the mean for each (2D) grid location?
Ivan Mich
il 27 Lug 2021
Rik
il 27 Lug 2021
@Ivan Mich Why are you ignoring my answer to your question and reposted it here as a comment?
You are also consistently ignoring the main question: what do you mean by merging?
Ivan Mich
il 27 Lug 2021
Image Analyst
il 27 Lug 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.
Categorie
Scopri di più su Creating and Concatenating Matrices 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!