How to find the number of groups in another group?
9 views (last 30 days)
Show older comments
If I have a matrix, for example,
0 1
0 2
0 3
0 1
0 2
0 3
0 4
0 5
0 1
0 2
1 1
1 2
1 3
1 4
1 1
1 2
1 3
Column 1 represents control (0) and experiment (1), while Column 2 indicates groups. Row 1-3 (1, 2, 3) is a group, Row 4-8 (1, 2, 3, 4, 5) is another group, Row 9-10 (1, 2) is another, etc.
How do I find how many groups there are in Control, and how many groups ther are in Experiment?
Thank you for your help!
0 Comments
Accepted Answer
Voss
on 7 Feb 2023
Edited: Voss
on 7 Feb 2023
M = [
0 1
0 2
0 3
0 1
0 2
0 3
0 4
0 5
0 1
0 2
1 1
1 2
1 3
1 4
1 1
1 2
1 3
];
The idea is to find how many times the difference between adjacent elements in column 2 of M is less than or equal to zero, because that indicates a change in groups, e.g., going from 3->1 or 5->1. (Append a 1 to the end to catch the last group.)
Do that twice, once where the first column of M is 0 and once where the first column of M is 1.
n_control_groups = nnz(diff([M(M(:,1) == 0, 2); 1]) <= 0)
n_experiment_groups = nnz(diff([M(M(:,1) == 1, 2); 1]) <= 0)
4 Comments
Voss
on 10 Feb 2023
Edited: Voss
on 10 Feb 2023
You can do it in a way that's similar to what you were doing.
This part is the same:
M = [
0 1 3
0 2 2
0 3 5
1 1 4
1 2 9
1 3 8
1 4 5
1 5 3
0 1 2
0 2 1
1 1 7
1 2 6
1 3 3
1 4 8
0 1 7
0 2 4
0 3 7
];
g = nan(size(M,1),1);
ix = M(:,2)==1;
g(ix) = 1:sum(ix);
g = fillmissing(g,'previous');
Use findgroups with two grouping variables: M(:,1) (control/experiment) and the g you already constructed:
[g_new,is_experiment] = findgroups(M(:,1),g)
g_new is the new group numbers based on those two variables, and is_experiment is the group IDs of the groups according to the first grouping variable, M(:,1) (so it has the zeros and ones from M(:,1)). (If you were to capture the third output from findgroups, it would be the group IDs of the groups according to the second grouping variable, g. See this link for an explanation of this usage.)
Then use g_new in splitapply, instead of g:
S = splitapply(@sum,M(:,3),g_new);
And finally use is_experiment to separate S:
S_control = S(is_experiment == 0)
S_experiment = S(is_experiment == 1)
More Answers (1)
Vilém Frynta
on 7 Feb 2023
Hello,
if every group always starts with number 1, you can try find their positions.
It would look like so:
idx = find(matrix(:,2) == 1) % idx = positions of all number 1
Now that you know the positions of each group and know that there are numbers in the first column that correspond to them, you can use those positions to find the corresponding numbers (0 or 1) in the first column.
Using something like this:
matrix(idx,1)
will give you a vector that consists of 0s and 1s. Now it's simple, use length() and sum() to find out how many 0s (control) and 1s (experiments) are there.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!