我有这样的数据:
1 0 1
1 1 1
0 1 1
1 1 1
1 1 1
1 1 1
1 1 0
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
0 0 1
1 1 1
1 1 1
1 1 1
每一列代表一个设备,每一行代表一个时间段。每个数据点表示该时间段内设备是否处于活动状态。我正在尝试计算每个设备连续处于活动状态的时间长度,或称为“连续时间”,即在每一列中连续的 1 的长度。在这种情况下,第一列应该是2 11 3
,依此类推。
对于单个设备(单列数据),这很容易实现:
rng(1)
%% Parameters
lambda = 0.05; % Pr(failure)
N = 1; % number of devices
T = 18; % number of time periods in sample
%% Generate example data
device_status = [rand(T, N) >= lambda ; false(1, N)];
%% Calculate spell lengths, i.e. duration of uptime for each device
cumul_status = cumsum(device_status);
% The 'cumul_status > 0' condition excludes the case where the vector begins with one
% or more zeros
cumul_uptimes = cumul_status(device_status == 0 & cumul_status > 0);
uptimes = cumul_uptimes - [0 ; cumul_uptimes(1:end-1)];
所以我可以简单地迭代遍历列,并逐一进行操作,使用parfor
(例如)并行运行。是否有一种方法可以使用向量化矩阵操作同时在所有列上执行此操作?
编辑:应该补充说明的是,每个设备可能具有不同数量的正常运行时间。
reshape(max(cumsum(cumprod(hankel([zeros(1,size(A,2));A])'))),size(A,1)+1,size(A,2));
其中 A 是矩阵。然后你就可以找到局部最大值,但有时使用循环更好更快。 - obchardon