how to seek and replace characters on cell
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
vx2008
am 27 Nov. 2015
Kommentiert: Geoff Hayes
am 28 Nov. 2015
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 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 27 Nov. 2015
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'
Weitere Antworten (1)
Stephen23
am 27 Nov. 2015
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 Kommentare
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!