如何使用Matlab在约束的单元格数组中选择相同的值? [英] How to choose identical values in a cell array with constraints using Matlab?

查看:3191
本文介绍了如何使用Matlab在约束的单元格数组中选择相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个4x1 Cell结构,代表 a = [A1 A2 A3 A4]

  a = cell(4,1​​)
a {1} = [1 3 1 0]
a {2} = [3 3 3 3]
a { 3} = [3 2 3 2]
a {4} = [3 3 3 2]

B = [1 1 1 2] %priority

我想执行以下操作:

选择与优先级 B = [1 1 1 2] (其中 B = 1 是最高优先级, A = 3

这意味着找到以[3 3 3#]开头的单元格,理想的答案应该是: a {code}



< 2}
= [3 3 3 3] a {4} = [3,3,3,2]






我的尝试是添加:

  [P arrayind] = min(B)%问题是arrayind返回索引= 1 indices 
if P == 1
arrayindex = 1:4; %查看数组的每个索引
c = a(cellfun(@(x)ismember(x(arrayindex),3),a));但是这给我一个错误,说明:




$ b 使用cellfun时出错
非标量在统一输出中,在索引1,输出1.
将'UniformOutput'设置为false。

通过调整代码以适应此错误:

  c = a(cellfun(@(x)ismember(x(arrayindex),3),a,'UniformOutput',false) 

我得到这个错误:

使用subsindex时出错
'cell'类的值没有定义函数'subsindex'。

现在我陷入了这一点。

解决方案

这可能不是一个优雅的答案,但它是有效的:

 %输入单元格数组。 
a = cell(4,1​​);
a {1} = [1 3 1 0];
a {2} = [3 3 3 3];
a {3} = [3 2 3 2];
a {4} = [3 3 3 2];

%输入优先级数组。
B = [1 1 1 2];

%输出单元格数组:preallocated为了效率。
c = cell(size(a));

j = 1;
for i = 1:size(a,1)
%对于每个i
if(all(((cell2mat(a(i))== 3)& 1))==(B == 1)))
%cell2mat将单元格数组转换为容易比较的数组数组。
%X == Y对于相同大小的矩阵,将给出一个相同大小的结果矩阵,其中值相等,其他值为0。
%因此,cell2mat(a(i))== 3将a {i}表示的数字矩阵与3进行比较。
%(cell2mat(a(i))== 3)&(B == 1)将与B == 1进行逻辑AND操作,即[1 1 1 0 ]。
%简而言之,因为你想要a {i}为3,当B为1时,我们想要那些a {i},其中上述比较与B == 1 。
%如果结果数组与B = 1相同,我们得到[1 1 1 1]作为比较结果((cell2mat(a(i))== 3)& ;(B == 1))==(B == 1)。
%函数all检查它的输入是否完全非零:这里如果我们得到一个[1 1 1 1],all将给我们1,否则0.
c {j} = a {i};
%条件满足时,将结果数组插入c。

j = j + 1;
%增加c的索引。
end
end


c = c(1:j-1);
%截断未使用的c行。

cell2mat(c)
%显示计算出的c的值。


If I have a 4x1 Cell structure with that represents a=[A1 A2 A3 A4] :

a=cell(4,1)
a{1}=[1 3 1 0]
a{2}=[3 3 3 3]
a{3}=[3 2 3 2]
a{4}=[3 3 3 2]

B=[1 1 1 2]; %priority

I would like to do the following :

Pick cells that correspond to priority B=[1 1 1 2] (where B=1 is highest priority and A=3)
Which means, find any cell that begins with [3 3 3 #], where all their priority is 1's in B.

ideal answer should be : a{2}=[3 3 3 3] and a{4} = [3,3,3,2]


My try is to add this :

[P arrayind]=min(B) % problem is that arrayind return index=1 only .. not all indices
if P==1
   arrayindex = 1:4 ; %look at each index of the array
   c = a(cellfun(@(x) ismember(x(arrayindex), 3), a));
end

However this gives me an error stating :

Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.

By adjusting the code to accommodate this error :

c = a(cellfun(@(x) ismember(x(arrayindex), 3), a,'UniformOutput',false));

I get this error :

Error using subsindex
Function 'subsindex' is not defined for values of class 'cell'.

And now I'm stuck at this point.

解决方案

This may not be an elegant answer, but it is effective:

%The input cell array.
a = cell(4,1);
a{1} = [1 3 1 0];
a{2} = [3 3 3 3];
a{3} = [3 2 3 2];
a{4} = [3 3 3 2];

%The input priority array.
B = [1 1 1 2];

%The output cell array: preallocated for efficiency.
c = cell(size(a));

j = 1;
for i = 1:size(a,1)
%For each i
    if(all(((cell2mat(a(i))==3)&(B==1))==(B==1)))
    %"cell2mat" converts the cell arrays into easily comparable number arrays.
    %"X==Y" for matrices of the same size, will give you a result matrix of the same size with 1 where the values are equal and 0 elsewhere.
    %Thus, "cell2mat(a(i))==3" would compare the number matrices represented by "a{i}" with "3".
    %"(cell2mat(a(i))==3)&(B==1)" would do a logical AND operation with "B==1", that is, "[1 1 1 0]".
    %In short, since you want whereever "a{i}" is 3 when "B" is 1, we want those "a{i}" where the comparison stated above is the same as "B==1".
    %If the result array is the same as "B=1", we get "[1 1 1 1]" as the result of the comparison "((cell2mat(a(i))==3)&(B==1))==(B==1)".
    %The function "all" checks whether the input to it is completely non-zero: here if we get a "[1 1 1 1]" "all" will give us 1, else 0.
        c{j} = a{i};
        %Insert result array into "c" when condition is satisfied.

        j = j + 1;
        %Increment the index of "c".
    end
end


c = c(1:j-1);
%Truncate unused rows of "c".

cell2mat(c)
%Displays the value of "c" as computed.

这篇关于如何使用Matlab在约束的单元格数组中选择相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆