how to seek and replace characters on cell

Now I have a mat A as below:
A=[1 1 1;1 2 2;2 1 2;2 2 1]
A =
1 1 1
1 2 2
2 1 2
2 2 1
Now want to replace '1' and '2' with 'a' and 'b' seperately; so I use function 'mat2cell()' to set mat A as cell B; but I don't know how shall I do the above replacement.

 Accepted Answer

Rather than converting the matrix to a cell array using mat2cell, consider applying a function to each element of A using arrayfun. Define a mapping for 1 and 2 and apply that mapping to each element. For example,
map = {'a', 'b'};
We will treat the elements of A as indices into the map as
B = arrayfun(@(x)map(x),A);
where
B =
'a' 'a' 'a'
'a' 'b' 'b'
'b' 'a' 'b'
'b' 'b' 'a'

6 Comments

vx2008's answer moved here
Thank you for your reply.
In the fact, mat A is more; I mean
1 1 1
1 2 2
2 1 2
2 2 1
is just a part of Mat A; other area is other number and I just want to replace this area number(it is the beginning of mat A).
So you have a matrix that you want to convert to a cell array of 1's, 2's, a's and b's. Is that correct?
vx2008
vx2008 on 27 Nov 2015
Edited: vx2008 on 27 Nov 2015
Thank you for your reply.
In the fact, mat A is more;
I want to import the data as below picture from excel
[data,str]=xlsread(file's path); then I get 'data' and 'str' as below:
Then I want to
replace 1 and 2 of data(1:4,1) by '45℃' and '55 ℃' of 'str';
replace 1 and 2 of data(1:4,2) by '2.2g' and '3.0g' of 'str';
replace 1 and 2 of data(1:4,3) by '5.0kg' and '7.0kg' of 'str';
so how shall I do better?
thank you!
Ok - since you will be mixing integers with strings (the temperatures and weights) then you will need to create a cell array. You can do that with data by
data = mat2cell(data,ones(1,size(data,1)),ones(1,size(data,2)))
The second and third inputs to mat2cell determine the distribution of the elements (so each element of our numeric matrix becomes an element within the cell array).
Now, get the mapping from str as
map = str(8:end,2:4);
(As an aside, you may want to rename your data and str to something more descriptive.)
All we need to do now is replace certain elements of data with their mapped equivalents. And we can do this for each column
for k=1:3
data(:,k) = map(cell2mat(data(:,k)),k)
end
So for each of the first three columns, we just apply the appropriate map.
Thank you again. I have get it according to your guiding.
About the code data = mat2cell(data,ones(1,size(data,1)),ones(1,size(data,2))), it can be instead by data=num2cell(data).
Now I have get the data as below:
data
data =
'45 ℃' '2.2g' '5.0kg' [ 46]
'45 ℃' '3.0g' '7.0kg' [ 53]
'55 ℃' '2.2g' '7.0kg' [ 60]
'55 ℃' '3.0g' '5.0kg' [ 50]
[ 99] [ 106] [ 96] [ 0]
[ 110] [ 103] [ 113] [ 0]
[ 13.4444] [ 1] [ 32.1111] [ 0]
[161.4476] [4.0522e+03] [1.6211e+04] [2.2500]
then I run the code as below:
f=figure('Position',[0 60 120+C*80,30+R*20]);
t=uitable(f,'Data',data{:},'ColumnName',cname,'RowName',rname);
Then the Matlab complained that:
Error using cell/strmatch (line 19)
Requires character array or cell array of strings as inputs.
Error in uitable_parseold (line 41)
if (~isempty(strmatch(lower(args{index}),
oldpropnames, 'exact')))
Error in uitable (line 40)
if (nargout == 2) ||(uitable_parseold(varargin{:}))
so maybe I still should make some modification about it. I think I should convert all number's cells of data into character cells; and then convert data matrix into 1X1 matrix. but I don't know how?
If you want to convert each element of the cell array to a string, then use cellfun to apply num2str to convert each number. For example, try
dataStr = cellfun(@(k)num2str(k),data,'UniformOutput',false);

Sign in to comment.

More Answers (1)

There is no need to use complicated arrayfun, simply use A as an index:
>> A = [1 1 1;1 2 2;2 1 2;2 2 1];
>> map = {'a','b'};
>> B = map(A)

Categories

Asked:

on 27 Nov 2015

Commented:

on 28 Nov 2015

Community Treasure Hunt

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

Start Hunting!