フィルターのクリア

Count number of unique values in column one, for each unique value in column 2

34 ビュー (過去 30 日間)
Colby
Colby 2015 年 9 月 29 日
コメント済み: Andrei Bobrov 2015 年 9 月 29 日
I've been faced with a problem that is giving me lots of trouble. I need to calculate the number of unique records in one column for each unique record in the second column. For example: If I have
Activities = {'walk', 'monday'; 'walk', 'monday'; 'eat', 'monday'; 'TV', 'tuesday'; 'run','wednesday'; 'eat', 'wednesday';...}
'call', 'Thursday';'work', 'Thursday'; 'call', 'Thursday';'work', 'Thursday';'sleep','Thursday'}
What I'd like in another column is the total number of unique activies for each unique day, formatted like "Result".
Result = [2;2;2;1;2;2;3;3;3;3;3]

採用された回答

Star Strider
Star Strider 2015 年 9 月 29 日
編集済み: Star Strider 2015 年 9 月 29 日
Expanding on Andrei Bobrov’s Answer to a related Question:
[~,~,ic1] = unique(Activities(:,1), 'stable'); % Unique Activities
[~,~,ic2] = unique(Activities(:,2), 'stable'); % Unique Days
H = accumarray([ic1 ic2], 1); % Histogram
LH = sum(H > 0); % Sum Logical Array
SH = sum(H); % Sum Event Array
Result = [];
for k1 = 1:length(LH)
Result = [Result; repmat(LH(k1), SH(k1), 1)]; % Create ‘Result’ Vector
end
  3 件のコメント
Star Strider
Star Strider 2015 年 9 月 29 日
My pleasure!
To be a bit more descriptive, LH is the sum of different events per day, and SH is the number of total events per day.
Andrei Bobrov
Andrei Bobrov 2015 年 9 月 29 日
[~,~,ic1] = unique(Activities(:,1), 'stable');
[~,~,ic2] = unique(Activities(:,2), 'stable');
H = accumarray([ic1 ic2], 1);
Result = repelem(sum(H > 0),sum(H));
or
H1 = sum(H > 0);
Result = H1(ic2);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by