Perform operation on character arrays

4 views (last 30 days)
How can I perform some operations on character cells? For example, after an if statement for a numeric cell array, I get another cell array that has characters instead of numeric values, for instance:
A{1,1}=[a,b,c;a,b,c;a,b,c]
A{1,1} corresponds to a cell array that has numerical values and I want to perform some operations for only the components of the numeric cell array that has "b"s not "a"s and "c"s in the character array.
For example, the original A{1,1} is:
A{1,1}=[1,2,3;4,5,6;7,8,9]
So I want to do some calculations for (2,5 and 8) only. My original cell array in large and I cannot select the positions of all "b"s individually and have to select them in a for loop automatically, so my question is how can I identify the index of these components and performsome some operation on them? Is this even possible?
  3 Comments
MarshallSc
MarshallSc on 12 Jul 2021
Hi Chris, thank you for your comment. I haven't used find but I will definitely use it to see whether I can identify the components that I need to do opertation on them.
Christopher McCausland
Christopher McCausland on 15 Jul 2021
No problem Marshall,
Sorry I didn't have more time to provide a fuller answer though I see Walter has it covered very well! 'find' and 'ismember' are great for char and table manipulation and can be really powerful in these kind of situations.
Christopher

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jul 2021
Example
C = {'a','b','c';'a','b','c';'a','b','c'}
C = 3×3 cell array
{'a'} {'b'} {'c'} {'a'} {'b'} {'c'} {'a'} {'b'} {'c'}
A{1,1} = [1,2,3;4,5,6;7,8,9];
A{1,2} = randi(9, 3, 3);
A{1,3} = randi(9, 3, 3);
celldisp(A)
A{1} = 1 2 3 4 5 6 7 8 9 A{2} = 8 4 8 2 2 4 1 7 9 A{3} = 1 8 3 9 8 1 2 3 3
mask = ismember(C, 'b')
mask = 3×3 logical array
0 1 0 0 1 0 0 1 0
%maybe you want to select only those values into separate arrays
selected_values = cellfun(@(c) c(mask), A, 'uniform', 0)
selected_values = 1×3 cell array
{3×1 double} {3×1 double} {3×1 double}
%maybe you want to sum the corresponding selected values
selected_sum = sum(cell2mat(selected_values),2)
selected_sum = 3×1
14 15 18
%maybe you want to multiply the selected values by something, leaving the
%rest untouched
times_N = randi(9)
times_N = 6
selected_multiplied = cellfun(@(c) c.*~mask + times_N.*c.*mask, A, 'uniform', 0)
selected_multiplied = 1×3 cell array
{3×3 double} {3×3 double} {3×3 double}
celldisp(selected_multiplied)
selected_multiplied{1} = 1 12 3 4 30 6 7 48 9 selected_multiplied{2} = 8 24 8 2 12 4 1 42 9 selected_multiplied{3} = 1 48 3 9 48 1 2 18 3
  3 Comments
Walter Roberson
Walter Roberson on 15 Jul 2021
Note: the code for selective multiplication here will fail for infinite values, turning them into nan.
MarshallSc
MarshallSc on 16 Jul 2021
Thanks Walter!
On a different note, I don't know if it's allowed to say it here, but I just wanted to thank you Walter for all of your kind and invaluable helps with the codes. You personally hepled me out a lot so I wanted to show you my deepes gratitude; your kindness and the time you put into helping others don't go unnoticed man! Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!