필터 지우기
필터 지우기

Counting the number of appearance of a value in a loop

조회 수: 1 (최근 30일)
federico nutarelli
federico nutarelli 2022년 7월 6일
댓글: Walter Roberson 2022년 7월 6일
Hi everyone,
I have the following matrix called all_indices_pesi in matlab:
consisting of the first column being a column of zeros and ones and the second column a column of indices which possibly repeat within the vector. Now my aim is the following:
  1. I would like to count the number of times an index appear in column 2 (so for insstance how many times does the element 78818 appear in the second column? How many times doess 78824 appears? and so on...). Let's call it num_instances;
  2. I would like to count the number of timess that a certain index in column 2 has a value of 1 in column 1. Let's call it num_ones;
  3. I would like to see, for each unique element of column 2 the share "num_ones/num_instances" and project it into a 646x151 matrix in the places indicated by the unique indices in column 2
Thank you

채택된 답변

Walter Roberson
Walter Roberson 2022년 7월 6일
편집: Walter Roberson 2022년 7월 6일
[G, value] = findgroups(all_indices_pesi(:,2));
count = accumarray(G, 1);
num_instances = table(value, count);
See findgroups documentation
See also grpstats
  댓글 수: 2
federico nutarelli
federico nutarelli 2022년 7월 6일
@Walter Roberson Thanks for the comment. How can I the wrap all up in a 646x151 matrix in the places sspecified by all_indices_pesi(:,2)? Thank you
Walter Roberson
Walter Roberson 2022년 7월 6일
If the 151 corresponds to the number of different values that can appear in column 1, then
[G2, value2] = findgroups(all_indices_pesi(:,2));
[G1, value1] = findgroups(all_indices_pesi(:,1));
count = accumarray([G2, G1], 1);
row #J corresponds to column 2 value value2(J)
column #K corresponds to column 1 value value1(K)
So count(J,K) is the count for column 2 value value2(J) in combination with column 1 value value1(K)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by