我有一个矩阵,其矩阵随机分布在整个矩阵中。我想要连续值的索引,而且,我想要一个与原始矩阵大小相同的矩阵,其中连续值的数量存储在连续值的索引中。例如
original_matrix = [1 1 1;2 2 3; 1 2 3];
output_matrix = [3 3 3;2 2 0;0 0 0];
我一直在努力寻找解决这个问题的方法。它与气象数据质量控制有关。例如,如果我有一个来自多个传感器的温度数据矩阵,并且我想知道哪些天具有恒定的连续值,以及多少天是恒定的,那么我可以将数据标记为可能有故障。
温度矩阵是天数x站数,我想要一个输出矩阵,它也是天数x站数,其中连续值如上所述被标记。
如果您有解决方案,请提供!谢谢。
答案 0 :(得分:1)
对于这类问题,我创建了自己的效用函数runlength
:
function RL = runlength(M)
% calculates length of runs of consecutive equal items along columns of M
% work along columns, so that you can use linear indexing
% find locations where items change along column
jumps = diff(M) ~= 0;
% add implicit jumps at start and end
ncol = size(jumps, 2);
jumps = [true(1, ncol); jumps; true(1, ncol)];
% find linear indices of starts and stops of runs
ijump = find(jumps);
nrow = size(jumps, 1);
istart = ijump(rem(ijump, nrow) ~= 0); % remove fake starts in last row
istop = ijump(rem(ijump, nrow) ~= 1); % remove fake stops in first row
rl = istop - istart;
assert(sum(rl) == numel(M))
% make matrix of 'derivative' of runlength
% don't need last row, but needs same size as jumps for indices to be valid
dRL = zeros(size(jumps));
dRL(istart) = rl;
dRL(istop) = dRL(istop) - rl;
% remove last row and 'integrate' to get runlength
RL = cumsum(dRL(1:end-1,:));
它仅适用于列,因为它使用linear indexing。由于你想在行上做类似的事情,你需要来回移调,所以你可以像你这样使用它:
>> original = [1 1 1;2 2 3; 1 2 3];
>> original = original.'; % transpose, since runlength works along columns
>> output = runlength(original);
>> output = output.'; % transpose back
>> output(output == 1) = 0; % see hitzg's comment
>> output
output =
3 3 3
2 2 0
0 0 0