如何在过去 10 年的工作日基础上计算二元事件(0 或 1)在 matlab 中的频率?
How to calculate the frequency of a binary event (0 or 1) on a weekday basis over the last 10 years in matlab?
我有一个包含 0 和 1 的单列逻辑矩阵和一个包含工作日名称的单列工作日字符矩阵。我想计算在两个矩阵的整个期间(都大约 10 年)每个工作日我得到 1 的次数。然后0相同。然后我想形象化它。有什么想法吗?
按照@Daniel 的建议使用unique
and accumarray
%// Random days and random 0s-1s created for sample
days ={'mon','tue','wed','tue','fri','wed','wed'}.'; %//'
A = randi([0 1],numel(days),1);
%// Concatenating to look it 'like' a table
In = [days,num2cell(A)]
%//Obtain index of unique 'days'
[~,uIdx,idx] = unique(days);
%// as accumarray in default sums up, we count the number of ones
count = accumarray(idx,A);
%// Concatenating to see the output 'like' a table as you wanted to visualize it
out = [days(uIdx),num2cell(count)]
结果:
In =
'mon' [1]
'tue' [1]
'wed' [1]
'tue' [1]
'fri' [0]
'wed' [0]
'wed' [1]
out =
'fri' [0]
'mon' [1]
'tue' [2]
'wed' [2]
我有一个包含 0 和 1 的单列逻辑矩阵和一个包含工作日名称的单列工作日字符矩阵。我想计算在两个矩阵的整个期间(都大约 10 年)每个工作日我得到 1 的次数。然后0相同。然后我想形象化它。有什么想法吗?
按照@Daniel 的建议使用unique
and accumarray
%// Random days and random 0s-1s created for sample
days ={'mon','tue','wed','tue','fri','wed','wed'}.'; %//'
A = randi([0 1],numel(days),1);
%// Concatenating to look it 'like' a table
In = [days,num2cell(A)]
%//Obtain index of unique 'days'
[~,uIdx,idx] = unique(days);
%// as accumarray in default sums up, we count the number of ones
count = accumarray(idx,A);
%// Concatenating to see the output 'like' a table as you wanted to visualize it
out = [days(uIdx),num2cell(count)]
结果:
In =
'mon' [1]
'tue' [1]
'wed' [1]
'tue' [1]
'fri' [0]
'wed' [0]
'wed' [1]
out =
'fri' [0]
'mon' [1]
'tue' [2]
'wed' [2]