How to find the number of groups in another group?
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!
Accepted Answer
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.
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!