Hi
I like to make matrix algorithms!
Here is one for grouping data in a column vector x:
The question is whether this is smart using the matrix functionality in Mata or not?
My intuition says that eg J(rows(x), 1, c) must be expensive in memory if x and c are large, but has this been taken care of by Stata?
The vector x could be build from st_data and hence be quite big!
Is there a more efficient way of doing grouping in Mata?
Looking forward to hear from you
I like to make matrix algorithms!
Here is one for grouping data in a column vector x:
Code:
: uniformseed(1234)
: x = uniform(20,1) * 100 // create data
: c = 5, 35, 65, 95 // Left group values
: // The actual grouping algorithm: group =1 if 5<=x<35, =2 if 35<=x<65, etc
: x, rowsum(J(rows(x), 1, c) :< x)
1 2
+-----------------------------+
1 | 47.701991 2 |
2 | 33.06433151 1 |
3 | 34.45906907 1 |
4 | 35.77345936 2 |
5 | 94.26101726 3 |
6 | 85.97270548 3 |
7 | 50.38059119 2 |
8 | 9.095890983 1 |
9 | 26.09330041 1 |
10 | 12.94203128 1 |
11 | 10.05143411 1 |
12 | 72.86309188 3 |
13 | 89.99825171 3 |
14 | 8.769400534 1 |
15 | 31.73631001 1 |
16 | 31.88967323 1 |
17 | 63.8018484 2 |
18 | 68.72346241 3 |
19 | 7.567738811 1 |
20 | 37.80273551 2 |
+-----------------------------+
My intuition says that eg J(rows(x), 1, c) must be expensive in memory if x and c are large, but has this been taken care of by Stata?
The vector x could be build from st_data and hence be quite big!
Is there a more efficient way of doing grouping in Mata?
Looking forward to hear from you

Comment