首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >某些条件下的约化矩阵.第2部分-

某些条件下的约化矩阵.第2部分-
EN

Stack Overflow用户
提问于 2015-04-06 20:08:07
回答 1查看 43关注 0票数 0

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

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

代码语言:javascript
运行
复制
V = {V1,V2,...,Vn}  (V1 or V2 or ...Vn)

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

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

这个问题的唯一初始数据MV

我想用一个for循环来回答问题here,但是我注意到计算时间随着V的大小而增加。

示例:

代码语言:javascript
运行
复制
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]} 

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

代码语言:javascript
运行
复制
[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]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-26 19:49:39

看看这对你有用吗-

代码语言:javascript
运行
复制
%// 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)))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29478819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档