Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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?

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 30 Mai 2016
Bearbeitet: Andrei Bobrov am 30 Mai 2016
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)];

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by