How to find the number of groups in another group?

조회 수: 9 (최근 30일)
Julia
Julia 2023년 2월 7일
편집: Voss 2023년 2월 10일
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!

채택된 답변

Voss
Voss 2023년 2월 7일
편집: Voss 2023년 2월 7일
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
Julia
Julia 2023년 2월 10일
Hi Voss, may I ask a follow up question? If my matrix looks like this instead, and I am calculating the sum for each group, how may I assign the sum of each group to corresponding control/experiment?
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');
S=splitapply(@sum,M(:,3),g)
S = 5×1
10 29 3 24 18
I want to split S into two variables, based on whether these sums belong to 0 or 1:
S_control = 10 3 18
S_experiment = 29 24
Would you please tell me if this is possible? If not, is there an alternative way to allocate elements in S to Control or Experiment?
Voss
Voss 2023년 2월 10일
편집: Voss 2023년 2월 10일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Vilém Frynta
Vilém Frynta 2023년 2월 7일
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.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by