Hi,
I would like to create a vector to index a group. Assume that I have the following data set:
a = { 1 1 1 50, 1 1 2 80, 1 2 1 60, 2 1 1 100, 2 1 2 90, 2 2 1 40, 2 2 2 50, 3 1 1 100, 3 1 2 70, 3 1 3 60, 3 2 1 20, 3 2 2 30, 3 2 3 30, 3 2 4 20 };
first col. indicating the country, second col. indicating the region within the country (each country has two regions) and the third col. is the city index. I would like to have an index for the unique regions. For this example it would be :
b = { 1, 1, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6 };
Any suggestions on how to do this for a large scale matrix?
Best
2 Answers
1
accepted
You can:
- Use a matrix-vector multiply to convert the first two (or any number) of columns into an integer id for the column.
- Find the unique integer id's.
- Convert the 2 digit integer id's to a list of sequential integer id's.
a = { 1 1 1 50, 1 1 2 80, 1 2 1 60, 2 1 1 100, 2 1 2 90, 2 2 1 40, 2 2 2 50, 3 1 1 100, 3 1 2 70, 3 1 3 60, 3 2 1 20, 3 2 2 30, 3 2 3 30, 3 2 4 20 }; // Convert first three columns into an integer, // i.e. { 1 1 } -> 11, { 3 1 } -> 31 ncols = 2; m = { 10, 1 }; grps = a[.,1:ncols]*m; // Get unique groups grps_unique = unique(grps); // Convert groups into sequential class groups idx = reclassify(grps, grps_unique, seqa(1, 1, rows(grps_unique)));
0
Thank you! I was not aware of the reclassify function. Up to that, I actually followed the same steps, but I was stuck with that.
Your Answer
2 Answers
You can:
- Use a matrix-vector multiply to convert the first two (or any number) of columns into an integer id for the column.
- Find the unique integer id's.
- Convert the 2 digit integer id's to a list of sequential integer id's.
a = { 1 1 1 50, 1 1 2 80, 1 2 1 60, 2 1 1 100, 2 1 2 90, 2 2 1 40, 2 2 2 50, 3 1 1 100, 3 1 2 70, 3 1 3 60, 3 2 1 20, 3 2 2 30, 3 2 3 30, 3 2 4 20 }; // Convert first three columns into an integer, // i.e. { 1 1 } -> 11, { 3 1 } -> 31 ncols = 2; m = { 10, 1 }; grps = a[.,1:ncols]*m; // Get unique groups grps_unique = unique(grps); // Convert groups into sequential class groups idx = reclassify(grps, grps_unique, seqa(1, 1, rows(grps_unique)));
Thank you! I was not aware of the reclassify function. Up to that, I actually followed the same steps, but I was stuck with that.