필터 지우기
필터 지우기

How do you transform a vector of numbers into a cell of strings?

조회 수: 1 (최근 30일)
Ben
Ben 2014년 6월 27일
댓글: Chang hsiung 2014년 6월 27일
I have a vector of numbers:
A = [1 2 3 4 5]';
I want to change it to a cell of strings, such as the one I've declared here:
B = cell(5,1); B{1} = '1'; B{2} = '2'; B{3} = '3'; B{4} = '4'; B{5} = '5';
How can I change A into B without using a for loop?

채택된 답변

Jos (10584)
Jos (10584) 2014년 6월 27일
A = [1:5].'
B = arrayfun(@(x) num2str(x),A,'un',0)

추가 답변 (1개)

Titus Edelhofer
Titus Edelhofer 2014년 6월 27일
Hi,
one possibility:
Acell = num2cell(A);
B = cellfun(@(x) sprintf('%g', x), Acell, 'UniformOutput', false);
Titus

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by