【问题标题】:Extracting portions of matrix into cell array将部分矩阵提取到单元格数组中
【发布时间】:2013-03-25 23:22:28
【问题描述】:

我有一个相当大的矩阵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;这不是我使用的实际数据。)

【问题讨论】:

  • 只是好奇,您使用的是什么实现?
  • 确实,我以前没有见过这种计算,所以我想知道你想达到什么目的。可能有更好的方法。
  • @DennisJaheruddin 我正在尝试从图像中提取字母。

标签: matlab matrix octave vectorization cell-array


【解决方案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

【讨论】:

  • +1:顺便说一下,使用strfind可以通过以下方式缩短索引的计算:startPoints = strfind([0 V], [0 1]); endPoints = strfind([V 0], [1 0]);
【解决方案2】:

我不确定的一件事是是否有更好的方法可以找到 being_indicesend_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

【讨论】:

  • 好吧,我刚刚针对 DMR 的 for 循环分析了 mat2cell 函数,它似乎 mat2cell 更慢...
猜你喜欢
  • 1970-01-01
  • 2017-09-23
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多