[英]How to find the row index of a cell array that matches certain row of cells in MATLAB?
說我有一個細胞陣列
CELLARRAY =
{{[]} {[]} {[1 1 1]} {[]} {[]} {[1 1 1]};
{[]} {[]} {[1 1 1]} {[]} {[]} {[]};
{[]} {[]} {[]} {[]} {[]} {[]}}
A = {{[]} {[]} {[1 1 1]} {[]} {[]} {[]}}
有沒有一種聰明的方法來找到CELLARRAY
中匹配A
的行索引? 我的答案是2?
最快的方法可能是遍歷行的for循環,而MATLAB的JIT編譯器應該能夠對此進行優化。
matches = false(1, size(CELLARRAY, 1));
for k = 1:size(CELLARRAY, 1)
matches(k) = isequal(CELLARRAY(k,:), A);
end
indices = find(matches);
或者,您可以使用諸如cellfun
類的cellfun
為您執行該操作,但速度可能會更慢
matches = cellfun(@(x)isequal(x, A), num2cell(CELLARRAY, 2));
indices = find(matches)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.