2

マトリックス全体にランダムに分布する一定の連続値を持つマトリックスがあります。連続する値のインデックスが必要です。さらに、連続する値の数が連続する値のインデックスに格納されている、元の行列と同じサイズの行列が必要です。例えば

  original_matrix = [1 1 1;2 2 3; 1 2 3];

  output_matrix = [3 3 3;2 2 0;0 0 0];

私はこの問題の解決策を見つけるのに非常に苦労しました。これは、気象データの品質管理に関連しています。たとえば、多数のセンサーからの温度データのマトリックスがあり、一定の連続した値を持つ日と一定の日数を知りたい場合、データに障害の可能性があるというフラグを立てることができます。

温度マトリックスは日数 x ステーション数であり、出力マトリックスも日数 x ステーション数であり、上記のように連続した値にフラグが付けられます。

解決策があれば教えてください!ありがとうございました。

4

1 に答える 1

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,:));

線形インデックスを使用するため、列に沿ってのみ機能します。行に沿って同様のことをしたいので、前後に転置する必要があるため、次のようにケースに使用できます。

>> 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
于 2014-11-09T12:19:50.987 に答える