【发布时间】:2016-03-11 16:50:04
【问题描述】:
我有一个包含字符串标识符的 4x1 元胞数组,以及一个 4x5 元胞数组,其中包含随着时间的推移这些实体中的每一个的 5 个数据点...
>> 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时不要使用'uni' 0标志。我认为你的问题是emptyIdx是一个单元格数组,它不能很好地进行索引。
标签: arrays matlab cell-array