Arrays 从MATLAB单元阵列中去除非数值数据

Arrays 从MATLAB单元阵列中去除非数值数据,arrays,matlab,cell-array,Arrays,Matlab,Cell Array,我有一个包含字符串标识符的4x1单元格数组,和一个包含这些实体随时间变化的5个数据点的4x5单元格数组 >> ids = { '1'; '2'; 'A'; '4' } ids = '1' '2' 'A' '4' >> vals = { 11, 12, 13, 14, 15; 21, 22, 23, 24, 25; 31, 32, 33, 34, 35;, 41, 42, 43, 44, 45} vals = [11] [12] [13]

我有一个包含字符串标识符的4x1单元格数组,和一个包含这些实体随时间变化的5个数据点的4x5单元格数组

>> ids = { '1'; '2'; 'A'; '4' }

ids = 

 '1'
 '2'
 'A'
 '4'

>> vals = { 11, 12, 13, 14, 15; 21, 22, 23, 24, 25; 31, 32, 33, 34, 35;, 41, 42, 43, 44, 45}

vals = 

 [11]    [12]    [13]    [14]    [15]
 [21]    [22]    [23]    [24]    [25]
 [31]    [32]    [33]    [34]    [35]
 [41]    [42]    [43]    [44]    [45]
我想将ID转换为数字,并去掉两个单元格数组中非数字ID的数据,留下:

ids = 

 [1]
 [2]
 [4]

vals = 

 [11]    [12]    [13]    [14]    [15]
 [21]    [22]    [23]    [24]    [25]
 [41]    [42]    [43]    [44]    [45]
我想知道这一点的关键是否在于找出哪些索引是空的,然后用这些索引对两个单元格数组进行寻址,但我不确定接下来该怎么做

>> numericIds = cellfun(@str2num, ids, 'un', 0)

numericIds = 

 [1]
 [2]
 []
 [4]

>> emptyIdx = cellfun(@isempty, numericIds, 'un', 0)

emptyIdx = 

 [0]
 [0]
 [1]
 [0]

>> ids(emptyIdx) = []
Error using subsindex
Function 'subsindex' is not defined for values of class 'cell'.

对于使用
emptyIdx
cellfun
的第二次调用,不要将
UniformOutput
标志指定为0。删除该选项,它将创建一个逻辑向量,您可以使用它直接索引到单元格数组中,并且可以删除条目

因此,您可以这样做:

emptyIdx = cellfun(@isempty, numericIds);
执行此操作后,可以使用逻辑索引删除受影响的行:

ids(emptyIdx) = [];
vals(emptyIdx,:) = [];

下面是一个运行示例:

>> ids = { '1'; '2'; 'A'; '4' }

ids = 

    '1'
    '2'
    'A'
    '4'

>> numericIds = cellfun(@str2num, ids, 'un', 0)

numericIds = 

    [1]
    [2]
    []
    [4]

>> emptyIdx = cellfun(@isempty, numericIds)

emptyIdx =

     0
     0
     1
     0

>> vals = { 11, 12, 13, 14, 15; 21, 22, 23, 24, 25; 31, 32, 33, 34, 35;, 41, 42, 43, 44, 45}

vals = 

    [11]    [12]    [13]    [14]    [15]
    [21]    [22]    [23]    [24]    [25]
    [31]    [32]    [33]    [34]    [35]
    [41]    [42]    [43]    [44]    [45]

>> vals(emptyIdx,:) = []

vals = 

    [11]    [12]    [13]    [14]    [15]
    [21]    [22]    [23]    [24]    [25]
    [41]    [42]    [43]    [44]    [45]

>> ids(emptyIdx) = []

ids = 

    '1'
    '2'
    '4'

对于使用
emptyIdx
cellfun
的第二次调用,不要将
UniformOutput
标志指定为0。删除该选项,它将创建一个逻辑向量,您可以使用它直接索引到单元格数组中,并且可以删除条目

因此,您可以这样做:

emptyIdx = cellfun(@isempty, numericIds);
执行此操作后,可以使用逻辑索引删除受影响的行:

ids(emptyIdx) = [];
vals(emptyIdx,:) = [];

下面是一个运行示例:

>> ids = { '1'; '2'; 'A'; '4' }

ids = 

    '1'
    '2'
    'A'
    '4'

>> numericIds = cellfun(@str2num, ids, 'un', 0)

numericIds = 

    [1]
    [2]
    []
    [4]

>> emptyIdx = cellfun(@isempty, numericIds)

emptyIdx =

     0
     0
     1
     0

>> vals = { 11, 12, 13, 14, 15; 21, 22, 23, 24, 25; 31, 32, 33, 34, 35;, 41, 42, 43, 44, 45}

vals = 

    [11]    [12]    [13]    [14]    [15]
    [21]    [22]    [23]    [24]    [25]
    [31]    [32]    [33]    [34]    [35]
    [41]    [42]    [43]    [44]    [45]

>> vals(emptyIdx,:) = []

vals = 

    [11]    [12]    [13]    [14]    [15]
    [21]    [22]    [23]    [24]    [25]
    [41]    [42]    [43]    [44]    [45]

>> ids(emptyIdx) = []

ids = 

    '1'
    '2'
    '4'

正如其他人所说,问题的根源在于您试图使用单元格数组作为索引。这是因为
cellfun
的输入决定是否返回单元格数组(
false
)的数字数组(
true

也就是说,我可能不会使用
cellfun
,而是直接在单元格数组中使用,以确定哪些是有效的数字(对于非数字,它返回一个NaN)

注意:通常,大多数对字符串进行操作的函数(如
str2double
)也对字符串的单元格数组进行操作


正如其他人所说,问题的根源在于您试图使用单元格数组作为索引。这是因为
cellfun
的输入决定是否返回单元格数组(
false
)的数字数组(
true

也就是说,我可能不会使用
cellfun
,而是直接在单元格数组中使用,以确定哪些是有效的数字(对于非数字,它返回一个NaN)

注意:通常,大多数对字符串进行操作的函数(如
str2double
)也对字符串的单元格数组进行操作


获取
emptyIdx
时,不要使用
'uni'0
标志。我认为您的问题在于
emptyIdx
是一个单元格数组,它不适合索引。在获取
emptyIdx
时,不要使用
'uni'0
标志。我认为您的问题在于
emptyIdx
是一个单元格数组,它不适合索引。非常感谢您花时间回答。很高兴看到我错在哪里了,尽管最后我给出了非cellfun的答案。非常感谢你花时间回答。很高兴看到我做错了什么,尽管最后我还是用了非cellfun的答案。