2012-01-16 78 views
3

我有一个单元格数组A,我想选择第一列(例如)的值为1234(对于例)。HowTo:选择单元格阵列中的所有行,其中特定列具有特定值

当A是不是一个单元阵列,我可以做到这一点:

B = A(A(:,1) == 1234,:); 

但是,当A为单元阵列,我收到此错误信息:

error: binary operator `==' not implemented for `cell' by `scalar' operations 

有谁知道要做到这一点,对于单元阵列

+0

''==甚至不执行为逐个细胞! – 2012-01-17 01:07:43

回答

0

我没有可用的时刻八度尝试一下,但我相信下面会做:

B = A(A {:1} == 1234,:);

处理cells()返回单元格时,{}返回单元格的内容。

0

问题是表达式a(:,1) == 1234(还有a{:,1} == 1234)。

例如:

octave-3.4.0:48> a 
a = 
{ 
    [1,1] = 10 
    [2,1] = 13 
    [3,1] = 15 
    [4,1] = 13 
    [1,2] = foo 
    [2,2] = 19 
    [3,2] = bar 
    [4,2] = 999 
} 
octave-3.4.0:49> a(:,1) == 13 
error: binary operator `==' not implemented for `cell' by `scalar' operations 
octave-3.4.0:49> a{:,1} == 13 
error: binary operator `==' not implemented for `cs-list' by `scalar' operations 

我不知道这是否是做的最简单,最有效的方式,但这个工程:

octave-3.4.0:49> cellfun(@(x) isequal(x, 13), a(:,1)) 
ans = 

    0 
    1 
    0 
    1 

octave-3.4.0:50> a(cellfun(@(x) isequal(x, 13), a(:,1)), :) 
ans = 
{ 
    [1,1] = 13 
    [2,1] = 13 
    [1,2] = 19 
    [2,2] = 999 
} 
相关问题