MATLAB-查找嵌套单元数组中项的多维索引

MATLAB-查找嵌套单元数组中项的多维索引,matlab,indexing,cell-array,Matlab,Indexing,Cell Array,在MATLAB中,假设我有一个单元格数组,如下所示: cell_arr = {{'a', 'b', 'c'}, {'d', 'e', 'f', 'g', 'h'}, {'a', 'b', 'c'}}; 我想要一种方法来查找单元格数组中出现的所有位置,例如,'a'。大概是 where(cell_arr, 'a'); % returns e.g., [[1 1] ; [3 1]] 我该怎么做 谢谢您的帮助。此解决方案可能并不简单,但会奏效。基本上,只需循环多维单元格数组中的每个单元格,并找到单词

在MATLAB中,假设我有一个单元格数组,如下所示:

cell_arr = {{'a', 'b', 'c'}, {'d', 'e', 'f', 'g', 'h'}, {'a', 'b', 'c'}};
我想要一种方法来查找单元格数组中出现的所有位置,例如,
'a'
。大概是

where(cell_arr, 'a'); % returns e.g., [[1 1] ; [3 1]]
我该怎么做


谢谢您的帮助。

此解决方案可能并不简单,但会奏效。基本上,只需循环多维单元格数组中的每个单元格,并找到单词的位置:

function location = where(cell_arr, word)

% initialize location
location = zeros(sum(char([cell_arr{:}]) == word),2);

% loop through cell_arr to find the location
count = 0;
for i = 1:length(cell_arr)
    for j = 1:length(cell_arr{i})
        if cell_arr{i}{j} == word
            count = count + 1;
            location(count,:) = [i j];
        end
    end
end
end
范例

where(cell_arr, 'a')
输出:

ans =
     1     1
     3     1

单元格数组的内容实际上是这样的单个字符,还是可能是单词或多个字符串?它们是字符向量形式的单词。所以,不是单个字符。谢谢