Ste*_*ing 0 arrays matlab vectorization cell-array
我有一个435x1单元阵列,其元素是'y','n'或'?'.我想找到哪些指数等于'y'.
对于普通数组,我只使用find函数.但是我不能将它用于单元格数组,因为没有为类型单元格定义eq.
我想我可以通过每个元素来做
for index=1:size(cell_array,1)
if cell_array{index} == 'y'
%add index to some array of indices
end
end
Run Code Online (Sandbox Code Playgroud)
但是有一种矢量化的方式来遍历数组并找到索引包含等于'y'的元素吗?任何帮助表示赞赏.
由于您知道每个单元格将包含单个字符,因此您可以连接所有单元格元素并执行单个矢量化测试:
find([cell_array{:}]=='y')
Run Code Online (Sandbox Code Playgroud)
可能最直接的方法就是使用strcmp
,它可以接受一个单元格数组作为第二个参数:
find(strcmp('y',cell_array))
Run Code Online (Sandbox Code Playgroud)