how to seek and replace characters on cell
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Geoff Hayes
2015-11-27
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'
更多回答(1 个)
Stephen23
2015-11-27
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!