MATLAB: Find matrix in cell matrix

find cel matrix

hello,
i have cell matrix size(1*1000) each element is matrix (10*10)
i want to test if matrix is an element of cell or not how to do it?

Best Answer

index = [];
for k = 1:numel(C)
if iseuqal(C{k}, test_matrix)
index = k;
break;
end
end
Or if you need all occurrences:
index = false(1, numel(C));
for k = 1:numel(C)
if isequal(C{k}, test_matrix)
index(k) = true;
end
end
found = find(index); % Or use the logical index vector directly
A shorter form:
found = find(cellfun(@(c) isequal(c, test_matrix), C));
At least under R2009a, the for loop method is 6 times faster for finding a 10x10 matrix in a 1 x 1000 cell array. cellfun is nice, but slow.