Find cell array and replace with another cell array in main cell array?
Show older comments
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 Comments
Cam Salzberger
on 3 Nov 2017
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
on 4 Nov 2017
Accepted Answer
More Answers (1)
Cam Salzberger
on 3 Nov 2017
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 Comments
Guillaume
on 3 Nov 2017
I have to agree with Cam. I don't see the need for a cell array in the example provided, so if the logical values are indeed stored in a cell array, I would convert that into a bog standard logical vector.
Following that, a simple strfind will locate your pattern (despite its name, strfind works fine with numerical or logical vectors).
If the K, L and M symbols do not stand for logical values, then I'd say that there is something fundamentally wrong in what you're doing. An heterogeneous container where most of the values are logical but a few are some other types really does not sound right. What is the use case?
Chenchal
on 3 Nov 2017
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?
R is a logical array, how to convert to cell array
how about num2cell( R)?
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!