필터 지우기
필터 지우기

How can I convert matrix to cell array of strings?

조회 수: 1 (최근 30일)
Ba Ba Black Sheep!
Ba Ba Black Sheep! 2017년 10월 3일
답변: KSSV 2017년 10월 3일
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?

채택된 답변

OCDER
OCDER 2017년 10월 3일
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)'

추가 답변 (1개)

KSSV
KSSV 2017년 10월 3일
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))

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by