在某些条件下减少矩阵 - 第2部分 -

时间:2015-04-06 20:08:08

标签: matlab matrix

这个问题比my previous question更复杂,因为这里的V是一个单元格

M是由若干子矩阵Ai组成的矩阵4x2000000,使得Ai(1:3,j)是j = 1,...,size(Ai,2)的相同向量。 Ai(4,j)1100之间的值。

V = {V1,V2,...,Vn}  (V1 or V2 or ...Vn)

V1,V2,... and Vn有不同的尺寸。

如果Ai不包含M的所有值,我的目标是消除Ai(4,:)的所有子矩阵V1 or V2 or ...Vn

此问题的唯一初始数据MV

我想在问题here的答案中使用for循环,但我注意到计算时间随着V的大小而增加。

示例:

M = [1022  3001  4451 1022 1022  3001 1022 3001 3001 1022 1055 1055 1055 1055 1055 1055;
      112    45    10  112  112    45   11   45   99  112   11   11   11   11   11   11;
      500    11    55  500  500    11   88   11    1  500   45   45   45   45   45   45;
        2     6     3    5   71     2    2   71    5   88    8   15   21   94   10   33] 



A1 = [1022 1022 1022 1022;
       112  112  112  112;
       500  500  500  500;
         2    5   71   88]

A2 = [3001 3001 3001;
        45   45   45;
        11   11   11;
         6    2   71]

A3 = [4451;
        10;
        55;
         3]

A4 = [1055 1055 1055 1055 1055 1055;
        11   11   11   11   11   11;
        45   45   45   45   45   45;
         8   15   21   94   10   33]

A5 =[3001;
       99;
        1;
        5]

if V = {[2 71],[3],[15 94 33 10]} 

预期输出(列的顺序并不重要):

[1022 1022 1022 1022 3001 3001 3001 4451 1055 1055 1055 1055 1055 1055;
  112  112  112  112   45   45   45   10   11   11   11   11   11   11;
  500  500  500  500   11   11   11   55   45   45   45   45   45   45;
    2    5   71   88    6    2   71    3    8   15   21   94   10   33]

1 个答案:

答案 0 :(得分:1)

看看这是否适合你 -

%// ID columns of M based on the uniquenes of the first thre rows
[~,~,idx] = unique(M(1:3,:).','rows')  %//'

%// Lengths of each V cell
lens = cellfun('length',V)

%// Setup ID array for use with ACCUMARRAY later on
id = zeros(1,sum(lens))
id(cumsum(lens(1:end-1))+1) = 1
id = cumsum(id)+1

%// Collect all cells of V as a 1D numeric array
Vn = [V{:}]

%// Counts of number of elements for each cell/groups of V
counts_V = histc(id,1:numel(V))

%// Function handle to detect for if the input would satisfy the crietria
%// of all its values belong to either V1 or V2 or ...Vn
func1 = @(x) any(counts_V == histc(id(ismember(Vn,x)),1:numel(V)))

%// For each ID in "idx", see if it satisfies the above mentioned criteria
matches = accumarray(idx(:),M(4,:)',[], func1 )  %//'

%// Use the "detections" for selecting the valid columns from M
out = M(:,ismember(idx,find(matches)))
相关问题