【问题标题】:How to choose identical values in a cell array with constraints using Matlab?如何使用 Matlab 在具有约束的单元格数组中选择相同的值?
【发布时间】:2013-03-22 17:21:21
【问题描述】:

如果我有一个代表a=[A1 A2 A3 A4] 的 4x1 单元结构:

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 #] 开头的单元格,其中它们的所有优先级在 B 中都是 1。

理想的答案应该是:a{2}=[3 3 3 3]a{4} = [3,3,3,2]


我的尝试是添加这个:

[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

但是这给了我一个错误说明:

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

通过调整代码来适应这个错误:

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'.

现在我被困在这一点上。

【问题讨论】:

  • 您是否尝试根据错误编辑cellfun 行?那是cellfun(@(x) ... ,'UniformOutput', false);
  • @natan 是的,我应该提到这一点。更新问题
  • 运行此代码后,您期望 c 的值是多少?
  • @wakjah 我更新了问题,所以更清楚了。结果应该是a{2}a{4}
  • @NLed 尝试:[val ind] = max(cellfun(@(x) length(find(ismember(x(arrayindex), 3))), a)); b = a(ind);。这将给出 3s 数量最多的元胞数组。

标签: matlab compare constraints combinations


【解决方案1】:

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

%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.

【讨论】:

  • 非常感谢您的回答。你能告诉我代码是如何工作的吗?我想学习而不是盲目地使用代码。
  • 当然可以。我会在答案中添加 cmets。
  • 出于好奇,如果我想检查更多值,这也可以吗?例如,如果B=2,则检查(2 or 3) 而不仅仅是3。我只需要更改
  • 如果您希望它同时适用于B=2B=3,请将B==1 替换为(B==1) | (B==2)(B==1) + (B==2)。这将使其对待 2 的方式与对待 3 的方式相同。
  • 是的。我暂时离开了。如果您还有任何疑问,请将它们留在下面的 cmets 中;我稍后再回来看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
相关资源
最近更新 更多