在Matlab单元格中查找索引

时间:2017-07-20 11:26:25

标签: matlab

在Matlab中考虑如下构造的单元data

clear all
Mrange=[2;4;3];
Mtot=sum(Mrange,1);
Nrange=[3;5;1];
Nsize=size(Nrange,1); %size(Nrange,1)=size(Mrange,1) by construction

data=cell(Nsize,1);
positions=cumsum(Mrange);
for n=1:Nsize
    if n==1    
       data{n} = kron((1 : 1 : Mrange(n))',ones(Nrange(n),1));
    else
       data{n} = kron((positions(n-1)+1 : 1 : positions(n-1)+Mrange(n))',ones(Nrange(n),1));
    end
end

%In short
%data{1} is a vector of dimension 2x3 listing [1;1;1;2;2;2] 
%(2 blocks as indicated by Mrange(1) with each number repeated 3 times as indicated by Nrange(1))

%data{2} is a vector of dimension 4x5 listing [3;3;3;3;3;4;4;4;4;4;5;5;5;5;5;6;6;6;6;6] 
%(4 blocks as indicated by Mrange(2) with each number repeated 5 times as indicated by Nrange(2))

%data{3} is a vector of dimension 2x3 listing [7;8;9] 
%(3 blocks as indicated by Mrange(3) with each number repeated once as indicated by Nrange(3))

现在假设我在Mtot1之间绘制Mtot个随机数

%Just for reproducibility suppose
indices=[2;2;1;9;8;7;2;7;1];

问题:我想构建维度indicesnew报告的单元格Nsizex1 n=1,...,Nsizeindicesdata{n}中的数字按indices中出现的顺序排列{/ 1}}。

因此

%indicesnew{1}=[2;2;1;2;1]
%indicesnew{2}=[]
%indicesnew{3}=[9;8;7;7]

此代码执行我想要的操作

indicesnew=cell(Nsize,1);
for m=1:Mtot
    for n=1:Nsize
        if ismember(indices(m),data{n})==1
            indicesnewtemp=indicesnew{n};
            indicesnew{n}=[indicesnewtemp; indices(m)];
        end
    end
end

你能否建议一个更高效的版本,可能没有循环?

1 个答案:

答案 0 :(得分:1)

虽然cellfun只是一个内部for循环,但这会删除你的一个循环并使你的代码更具可读性。当我在Linux Ubuntu计算机上测试运行时间(带有10000个循环和tic - toc)时,它运行得更快。

indicesnew = cellfun(@(x,y) {y(ismember(y,x))}, data, repmat({indices},3,1));