フィルターのクリア

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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by