現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Error in splitapply command
9 ビュー (過去 30 日間)
古いコメントを表示
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
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
2021 年 2 月 27 日
編集済み: Ivan Mich
2021 年 2 月 27 日
Ok , my full code is:
edges=1:0.5:10
[N, edges, bin] = histcounts(B, edges);
Result = accumarray(bin, A);
Y = discretize(B,edges)
% Y=rmmissing(Y)
mean_IMM=splitapply(@mean, A, bin) %mean
I am attaching the data.txt file (A id first column, B is second column).
I would appreciate if you could help me
Walter Roberson
2021 年 2 月 27 日
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
2021 年 2 月 27 日
Ok. If I want to throw out values <1 of B column and simultaneusly the corresponding values of column A how could I make it?
Ivan Mich
2021 年 2 月 27 日
Also I have one problem. I have change edges=0:0.5:10, but command window shows me:
Error using splitapply (line 111)
For N groups, every integer between 1 and N must occur at least once in the vector of
group numbers.
How could I fix it?
I am attaching file
Image Analyst
2021 年 2 月 27 日
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
2021 年 2 月 27 日
This is the rest code
filename1= 'data.txt'
[d1,tex]= importdata(filename1);
A=d1.data(:,1);
%B is a result from an equation including A values.
Image Analyst
2021 年 2 月 27 日
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?
Image Analyst
2021 年 2 月 27 日
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
2021 年 2 月 27 日
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
2021 年 2 月 28 日
Thank you, nut my question still exist. How can I throw out /reject some data.
For example, if I want to throw out values <1 of B column and the corresponding to them A values which command should I use?
Image Analyst
2021 年 2 月 28 日
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
2021 年 7 月 26 日
I would like to make a joint splitapply command. I mean I would like to
1) Group my data into bins from edges : 1:0.5:10 (min=1, max=10 with step equal to 0.5)
2) Compute the means of the values in each bin (lets call it set1).
3) Group my data into bins from edges : 2:1:10 (min=2, max=10 with step equal to 1)
4) Compute the means of the values in each bin (set2).
5) Merge two sets of data
Could you please help me in order to make it?
Thanking you in advance
Walter Roberson
2021 年 7 月 27 日
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
2021 年 7 月 27 日
the data are slightly different as the classes are different and therefore the averages in each bin. I would like to combine these two so that the results of the averages are smoothed out (tin order to make overlapping to my results). is it possible to do it?
Rik
2021 年 7 月 27 日
@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
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
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 Center および File Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)