Count the number of occurrences of elements in second column

6 ビュー (過去 30 日間)
Colby
Colby 2015 年 9 月 29 日
編集済み: Colby 2015 年 9 月 29 日
Hello, I'd like to know the number of unique entries, per unique entry in second column. For example, how could I return the number of unique activities per day.
Thanks so much for your time and help!
Activities = {'run', 'monday'; 'run', 'monday'; 'eat', 'monday'; 'TV', 'tuesday'; 'run','wednesday'; 'eat', 'wednesday'}
So in this example, I'd like the total number of unique actives, per day. So my desired result would look like "Result".
Result = [2;2;2;1;2;2]

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 9 月 29 日
[~,~,a]=unique(Activities(:,2));
b = histc(a,1:a(end));
Result = b(a);
  2 件のコメント
WAT
WAT 2015 年 9 月 29 日
Assuming I'm reading the code right, it won't distinguish between a repeated activity on the same day. So 2 entries of a "walk" on "Monday" would get counted as 2, rather than a 1.
Colby
Colby 2015 年 9 月 29 日
Thanks a lot guys!

サインインしてコメントする。

その他の回答 (1 件)

WAT
WAT 2015 年 9 月 29 日
Try something like
[uniqNames, ia, ic] = unique(Activities(:,2)); % find unique days
ia = [ia; 1+length(ic)];
Result = zeros(length(uniqNames)); % initialize output
for i=1:length(uniqNames) % loop through days
day = Activites( ia(i):ia(i+1), : ); % get all entries for that day
Result(i) = length(unique(day(:,1))); % count unique activities per day
end

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by