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... % codeA = {true;true;true;false;false;true;true;true;false;true;true;true;false;false;false};%logical row vectorB = {true,true,true};%logical col vectorC = {false,false,false};% assumption: replacement must also be logical col vector% Convert all to strings, use regexprep, convert backRchar = regexprep(num2str(cell2mat(A))',num2str(cell2mat(B)')',num2str(cell2mat(C)')');% Have to use _arrayfun_ since the output type must also be a cell arrayR = arrayfun(@(x) {logical(x)},str2num(char(split(Rchar,''))))';based in Guillaume comment: % codeA = {true;true;true;false;false;true;true;true;false;true;true;true;false;false;false};%logical row vectorB = {true,true,true};%logical col vectorC = {false,false,false};% assumption: replacement must also be logical col vectorR = logical(strrep(cell2mat(A'),cell2mat(B),cell2mat(C)))%need to convert logical array to cell array ??Based on Cam comment:%codeRchar2 = regexprep(char(cell2mat(A)+'0')',char(cell2mat(B)+'0'),char(cell2mat(C)+'0'));R2 = arrayfun(@(x) {logical(x)},str2num(char(split(Rchar2,''))))'; Related SolutionsMATLAB: Detecting TRUE elements of a cell array y={[true; false] false; false] [false; true] [true; false] false; true] [false; false] true false false false false true} out1= cellfun(@(x) all(x),y) out2= cellfun(@(x) any(x),y) MATLAB: How to replace a single word in a string with multiple row string This is possibly what you want:B = 'Some string with a Keyword in it';C = {'foo', 'bar', 'baz'};A = cellfun(@(rep) strrep(B, 'Keyword', rep), C, 'UniformOutput', false)If your C is a 2D char array and you also want a 2D char array as an outputB = 'Some string with a Keyword in it';C = ['foo';'bar';'baz'];A = cell2mat(cellfun(@(rep) strrep(B, 'Keyword', rep), num2cell(C, 2), 'UniformOutput', false)) Related QuestionDoes the cellfun failsMatLab Logical answer always 1Divide a cell arrays with a part of another cell arrayProblem with uitable with Logical cell and default value of ‘true’How could it be determined one array of a cell containing an string or a numberHow to create an uitable with one column as strings and second column as checkboxes
Best Answer