MATLAB: Find cell array and replace with another cell array in main cell array

findsetofelementincell

I have 3 cell arrays. A={true;true;true;false;false;true;true;true;false;true;true;true;false;false;false}; How can I find B={true,true,true}; in A
and how can I replace that founded part with C={K;L;M};??

Best Answer

Not efficient -- see below...
% code

A = {true;true;true;false;false;true;true;true;false;true;true;true;false;false;false};%logical row vector

B = {true,true,true};%logical col vector

C = {false,false,false};% assumption: replacement must also be logical col vector

% Convert all to strings, use regexprep, convert back
Rchar = regexprep(num2str(cell2mat(A))',num2str(cell2mat(B)')',num2str(cell2mat(C)')');
% Have to use _arrayfun_ since the output type must also be a cell array
R = arrayfun(@(x) {logical(x)},str2num(char(split(Rchar,''))))';
based in Guillaume comment:
% code
A = {true;true;true;false;false;true;true;true;false;true;true;true;false;false;false};%logical row vector
B = {true,true,true};%logical col vector
C = {false,false,false};% assumption: replacement must also be logical col vector
R = logical(strrep(cell2mat(A'),cell2mat(B),cell2mat(C)))
%need to convert logical array to cell array ??
Based on Cam comment:
%code
Rchar2 = regexprep(char(cell2mat(A)+'0')',char(cell2mat(B)+'0'),char(cell2mat(C)+'0'));
R2 = arrayfun(@(x) {logical(x)},str2num(char(split(Rchar2,''))))';