0

私はかなり大きな行列Mを持っていて、いくつかの列にしか興味がありません。Vの値が1対象の列を表すブールベクトルがあります。例:

      -1 -1 -1  7  7 -1 -1 -1  7  7  7
M  =  -1 -1  7  7  7 -1 -1  7  7  7  7
      -1 -1  7  7  7 -1 -1 -1  7  7 -1

V  =   0  0  1  1  1  0  0  1  1  1  1

の複数の隣接する値Vがすべて1である場合、の対応する列Mを別のマトリックスに抽出する必要があります。これは、前の行列を使用した例です。

      -1  7  7             -1  7  7  7
M1  =  7  7  7       M2  =  7  7  7  7
       7  7  7             -1  7  7 -1

どうすればこれを効率的に行うことができますか?行列のこれらすべての部分をMセル配列に格納するか、少なくとも次々にそれらを生成する効率的な方法が必要です。現在、これをwhileループで実行していますが、思ったほど効率的ではありません。

(私の例には値のみが含まれており、わかりやすくするために注意してください-17これは、私が使用する実際のデータではありません。)

4

2 に答える 2

1

diffこのための関数を利用して、Vベクトルをブロックに分割できます

% find where block differences exist
diffs = diff(V);
% move start index one value forward, as first value in
% diff represents diff between first and second in original vector
startPoints = find(diffs == 1) + 1;
endPoints = find(diffs == -1);

% if the first block begins with the first element diff won't have
% found start
if V(1) == 1
    startPoints = [1 startPoints];
end

% if last block lasts until the end of the array, diff won't have found end
if length(startPoints) > length(endPoints)
    endPoints(end+1) = length(V);
end

% subset original matrix into cell array with indices
results = cell(size(startPoints));
for c = 1:length(results)
    results{c} = M(:,startPoints(c):endPoints(c));
end
于 2013-03-26T00:20:47.350 に答える
1

私が確信していないことの1つは、being_indicesとを見つけるためのより良い方法があるかどうかですend_indices

コード:

X = [1     2     3     4     5     1     2     3     4     5
     6     7     8     9    10     6     7     8     9    10
    11    12    13    14    15    11    12    13    14    15
    16    17    18    19    20    16    17    18    19    20
     1     2     3     4     5     1     2     3     4     5
     6     7     8     9    10     6     7     8     9    10
    11    12    13    14    15    11    12    13    14    15
    16    17    18    19    20    16    17    18    19    20];

V = logical([ 1     1     0     0     1     1     1     0     1    1]);

find_indices = find(V);
begin_indices = [find_indices(1)  find_indices(find(diff(find_indices) ~= 1)+1)];
end_indices = [find_indices(find(diff(find_indices) ~= 1)) find_indices(end)];

X_truncated = mat2cell(X(:,V),size(X,1),[end_indices-begin_indices]+1);
X_truncated{:}

出力:

ans =

     1     2
     6     7
    11    12
    16    17
     1     2
     6     7
    11    12
    16    17


ans =

     5     1     2
    10     6     7
    15    11    12
    20    16    17
     5     1     2
    10     6     7
    15    11    12
    20    16    17


ans =

     4     5
     9    10
    14    15
    19    20
     4     5
     9    10
    14    15
    19    20
于 2013-03-26T01:06:21.170 に答える