# How to find the number of groups in another group?

6 Ansichten (letzte 30 Tage)
Julia am 7 Feb. 2023
Bearbeitet: Voss am 10 Feb. 2023
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?
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Voss am 7 Feb. 2023
Bearbeitet: Voss am 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_control_groups = 3
n_experiment_groups = nnz(diff([M(M(:,1) == 1, 2); 1]) <= 0)
n_experiment_groups = 2
##### 4 Kommentare3 ältere Kommentare anzeigen3 ältere Kommentare ausblenden
Voss am 10 Feb. 2023
Bearbeitet: Voss am 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 = 17×1
1 1 1 4 4 4 4 4 2 2
is_experiment = 5×1
0 0 0 1 1
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_control = 3×1
10 3 18
S_experiment = S(is_experiment == 1)
S_experiment = 2×1
29 24

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Vilém Frynta am 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 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Whos in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by