필터 지우기
필터 지우기

How to count iterations of an element in a column according to elements in another column?

조회 수: 1 (최근 30일)
Hello,
Attached you find a matrix I'm working with in R2012a. Column 1 corresponds to participant ID, 2 corresponds to questions, 3 to responses. Step 1: I am interested in finding a way to count how many times each question in column 2 was given a response of 1 or 2 OR 5 or 6 in column 3 (is there such a thing as a 'count' function?) Step 2: I am interested in putting the results of step 1 in another 49x3 matrix in which column 1 will correspond to question number, column 2 the number of times each question was given a response of 1 or 2 and column 3 the number of times each question was given a response of 5 or 6 across all participants (that is, ignore column 1 of hh.mat).
I can think of ways of doing this but I am stuck. I know I have to create a for loop that goes through each and every one of the 49 values in column 2. I know that I will have to create 2/more if statements (1 statement corresponding to instances in which a value in column 2 was given a rating of 1 | 2 in column 3 and another one for instances in which a value in column 2 was given a rating of 5 | 6). I've been trying stuff around but I'm a bit lost. Could you please be so kind as to help?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 5월 30일
편집: Andrei Bobrov 2016년 5월 30일
d = unique(hh(:,2));
a = ismember(hh(:,3),1:2);
b = ismember(hh(:,3),4:5);
step1 = sum(a + b);
step2 = [d, accumarray(hh(:,2),a), accumarray(hh(:,2),b)];
  댓글 수: 1
Bianca Elena Ivanof
Bianca Elena Ivanof 2016년 5월 30일
Excellent, Andrei!
The solution is so simple - my solution was a bit too complicated I guess, haha.
Many thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by