How can I convert matrix to cell array of strings?

1 Ansicht (letzte 30 Tage)
Ba Ba Black Sheep!
Ba Ba Black Sheep! am 3 Okt. 2017
Kommentiert: Stephen23 am 19 Sep. 2024
I want to convert the following:
mat = [1 2 3; 4 5 6 ; 7 8 9];
into the following array of strings,
arr = {'1,2(3)', '4,5(6)', '7,8(9)'};
How can I do that?
  1 Kommentar
Stephen23
Stephen23 am 19 Sep. 2024
Since R2016b:
mat = [1,2,3; 4,5,6; 7,8,9];
compose('%u,%u(%u)',mat)
ans = 3x1 cell array
{'1,2(3)'} {'4,5(6)'} {'7,8(9)'}

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

OCDER
OCDER am 3 Okt. 2017
You could use sprintf.
mat = [1 2 3; 4 5 6 ; 7 8 9];
arr = cell(1, size(mat, 1));
for k = 1:numel(arr)
arr{k} = sprintf('%d,%d(%d)', mat(k,:));
end
arr =
'1,2(3)' '4,5(6)' '7,8(9)'

Weitere Antworten (1)

KSSV
KSSV am 3 Okt. 2017
mat = [1 2 3; 4 5 6 ; 7 8 9];
arr = {'1,2(3)', '4,5(6)', '7,8(9)'};
nx = size(mat,1) ;
iwant = strcat(num2str(mat(:,1)),',',num2str(mat(:,2)),repelem('(',nx,1),num2str(mat(:,3)),repelem(')',nx,1))

Kategorien

Mehr zu Characters and Strings 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!

Translated by