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

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 日

2 投票

A = [1:5].'
B = arrayfun(@(x) num2str(x),A,'un',0)

2 件のコメント

Titus Edelhofer
Titus Edelhofer 2014 年 6 月 27 日
You are right. Working with arrayfun directly is much better ;-).
Chang hsiung
Chang hsiung 2014 年 6 月 27 日
great

サインインしてコメントする。

その他の回答 (1 件)

Titus Edelhofer
Titus Edelhofer 2014 年 6 月 27 日

0 投票

Hi,
one possibility:
Acell = num2cell(A);
B = cellfun(@(x) sprintf('%g', x), Acell, 'UniformOutput', false);
Titus

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

Ben
2014 年 6 月 27 日

コメント済み:

2014 年 6 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by