MATLAB: Find rows of cell array containing certain specific multiple cells

findMATLAB

Suppose I have a cell array,
ca = {'a' 'b' 'c' 10 10; 'c' 'e' 'f' 10 20; 'g' 'h' 'i' 20 10;}
How can I find the row containing eg. {'a'} {'b'} {'c'} at same time?

Best Answer

Hi,
try to run following code, variable 'r' returns you row that contains what are you looking for:
[r c] = find(strcmp(ca,'g'));
Hope this helps.
Edit:
ValueToFind = {'g' 'h' 'i'}; %Define cell array of values to find in same row
l = length(ValueToFind);
ca = {'a' 'b' 'c' 10 10; 'c' 'e' 'f' 10 20; 'g' 'h' 'i' 20 10;};
[r_ca ~] = size(ca);
Row = zeros(r_ca,l);
for ii = 1:l
[r ~] = find(strcmp(ca,ValueToFind(ii)));
if ~isempty(r)
r = sort(r);
Row(r,ii) = 1;
end
end
r_f = find(all(Row,2));
In this way you obtain the row (r_f) where all ValueToFind are. Right now I give you this solution, by adopting a for loop cycle. I dont think that exists a one-line solution.