Group values in a double & create new matrix according to the groups formed

3 ビュー (過去 30 日間)
Maria
Maria 2014 年 8 月 25 日
コメント済み: Andrei Bobrov 2014 年 8 月 26 日
I have a double variable A with 1 column and 30000 rows:
A=[44
12
79
12
83
63
53]
I applied the following code:
groupA=cell2mat(A(:,1));
[~, ~, ugroupA] = unique(groupA)
DA=dummyvar(ugroupA);
And I obtained a double matrix with 30000 rows and 262 columns with ones and zeros. The number of columns -262 - reflects the different numbers of the variable A.
What I am trying to do now is to apply basically the same code, but instead of getting a new column of ones and zeros for each different number in A, I would like to have a different column for the values between:
0 to 5
5 to 10
10 to 15
15 to 20
20 to 30
30 to 40
40 to 50
50 to 100
100 to 150
150 to 200
200 to 300
So instead of obtaining a new variable DA with 30000 rows and 262 columns, I would get a variable with 30000 rows but 11 columns.
Can someone help me please? Thank you

採用された回答

Kelly Kearney
Kelly Kearney 2014 年 8 月 25 日
xedge = [0 5 10 15 20 30 40 50 100 150 200 300];
A = rand(1000,1)*300;
[n, idx] = histc(A, xedge);
isin = bsxfun(@eq, idx, 1:length(xedge)-1);
Not entirely sure what your dummyvar was, but I think this gets to the 1/0 matrix you wanted.

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 8 月 25 日
編集済み: Andrei Bobrov 2014 年 8 月 26 日
EDIT
v = [0
5
10
15
20
30
40
50
100
150
200
inf];
[~,b] = histc(A,v);
out = dummyvar(b);
  3 件のコメント
Maria
Maria 2014 年 8 月 25 日
I introduced cell2mat and it solved this error, but then when I do
out = dummyvar(b);
I get another error:
Error using dummyvar (line 85)
Each element of GROUP must be a positive integer.
Andrei Bobrov
Andrei Bobrov 2014 年 8 月 26 日
corrected

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnalysis of Variance and Covariance についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by