フィルターのクリア

Find cell array and replace with another cell array in main cell array?

3 ビュー (過去 30 日間)
Ali Tunahan EROL
Ali Tunahan EROL 2017 年 11 月 3 日
コメント済み: Ali Tunahan EROL 2017 年 11 月 4 日
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};??
  2 件のコメント
Cam Salzberger
Cam Salzberger 2017 年 11 月 3 日
Can I ask why you are using a cell array? Are K, L, and M all logical values as well?
If that's the case, generally a logical array would be more memory-efficient, parsing time-efficient, and allow for simpler code.
Ali Tunahan EROL
Ali Tunahan EROL 2017 年 11 月 4 日
Actually, when I was writing the question, I made mistakes. A and B are cell arrays that are formed by logical values and C is cell array that is formed by chars. And also, I want the result as cell array with chars and ' '.

サインインしてコメントする。

採用された回答

Chenchal
Chenchal 2017 年 11 月 3 日
編集済み: Chenchal 2017 年 11 月 3 日
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,''))))';
  3 件のコメント
Cedric
Cedric 2017 年 11 月 3 日
Careful, STRREP and REGEXPREP are not working the same way. Regexp engines "eat" the string, STRREP does not:
>> strrep( 'aaa', 'aa', 'bb' )
ans =
'bbbb'
>> regexprep( 'aaa', 'aa', 'bb' )
ans =
'bba'
and STRFIND operates like STRREP:
>> strfind( [true,true,true], [true,true] )
ans =
1 2
Ali Tunahan EROL
Ali Tunahan EROL 2017 年 11 月 4 日
Actually I made mistakes when I was writing the question but I use that line: "R = logical(strrep(cell2mat(A'),cell2mat(B),cell2mat(C)))" and erase logical function.
R = transpose(strrep(cell2mat(transpose(A)),cell2mat(transpose(B)),cell2mat(transpose(C))))
this code gave me what I want.
Thank you!!

サインインしてコメントする。

その他の回答 (1 件)

Cam Salzberger
Cam Salzberger 2017 年 11 月 3 日
Ali,
Regardless of your reason for using cell arrays initially, it is likely to be faster to convert them to logical arrays before searching for the pattern. Chenchal shows you how to do that with cell2mat.
However, I would not recommend converting the logical values into character representations of their values (i.e. converting true into '1' and false into '0'). The conversion from number to character and back takes significant time that is likely to be unnecessary.
Once you have your logical arrays, you can use any of the algorithms mentioned in this blog post to find the location of the patterns, and then replace them with simple indexing and assignment.
If you would like to use any string-search functionality, rather than using num2str, I'd recommend just converting the logical values directly to some character values. It doesn't really matter which character value it is, so long as you can do matching with it. For easy of examination though, you could just do this:
charArray = char(logicalArray+'0');
Hope this helps!
-Cam
  3 件のコメント
Chenchal
Chenchal 2017 年 11 月 3 日
Hey thank you Guillaume, I did not realize that! So below should do:
% 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)))
R is a logical array, how to convert to cell array?
KL
KL 2017 年 11 月 3 日
編集済み: KL 2017 年 11 月 3 日
R is a logical array, how to convert to cell array
how about num2cell( R)?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by