フィルターのクリア

How to convert a vector of integers to a vector of characters?

11 ビュー (過去 30 日間)
Michael
Michael 2013 年 2 月 9 日
Hi - I have a vector of ints, typically 0/1, like this:
[0 0 0 0 1 0 0 0 0 1]
and I want to get this:
['0' '0' '0' '0' '1' '0' '0' '0' '0' '1']
But I cannot figure it out for the life of me. I'm sure it's simple. Thanks for your time and help.

採用された回答

James Tursa
James Tursa 2013 年 2 月 10 日
Another method for single digits:
A = [0 0 0 0 1 0 0 0 0 1];
B = char(A+'0');

その他の回答 (3 件)

Wayne King
Wayne King 2013 年 2 月 9 日
編集済み: Wayne King 2013 年 2 月 9 日
A = [0 0 0 0 1 0 0 0 0 1];
B = num2str(A);
Now B is a vector of characters
  2 件のコメント
Michael
Michael 2013 年 2 月 9 日
Hi Wayne -- I knew of num2str and tried that, but it turns out you're right. But what I figured out was that it was the extra spaces in the result of num2str that was throwing me off. i.e. num2str([0 0 0 0 0]) = '0 0 0 0 0', which is length 9 or something (but not 5). I removed the spaces from the result of num2str and it worked great. Thanks for the help.
Wayne King
Wayne King 2013 年 2 月 9 日
glad it helped

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


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 9 日
編集済み: Azzi Abdelmalek 2013 年 2 月 9 日
a = [0 0 0 0 1 0 0 0 0 1];
y=arrayfun(@(x) cellstr(num2str(x)),a)
% To get any character
y{4} % for e.g

Jan
Jan 2013 年 2 月 10 日
編集済み: Jan 2013 年 2 月 10 日
Instead of removing the spaces from the output of num2str, you can create string without spaces directly:
a = [0 0 0 0 1 0 0 0 0 1];
s = sprintf('%d', a);
Another fast but more strange method is:
s = char('0' + a);

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by