How to concatenate each row of a matrix into a vector ?

8 ビュー (過去 30 日間)
Valentin Cayron
Valentin Cayron 2019 年 2 月 22 日
回答済み: James Tursa 2019 年 2 月 22 日
Hey there,
I have a matrix 1000x7 of double (only 0 and 1) like that :
0 0 1 1 0 0 0
0 0 1 1 0 0 0
1 0 1 1 0 0 0
...
0 0 1 1 0 0 0
0 0 1 1 0 0 1
0 0 1 0 0 0 0
For each row, I have to concatenate each column value to get a vector of strings like this
0011000
0011000
1011000
...
0011000
0011001
0010000
Do you have any idea of how could I do that ?

採用された回答

Adam Danz
Adam Danz 2019 年 2 月 22 日
編集済み: Adam Danz 2019 年 2 月 22 日
% create fake data
A = [0 0 1 1 0 0 0
0 0 1 1 0 0 1
0 0 1 0 0 0 0];
% Convert to char array
s = arrayfun(@(x)sprintf('%d',x), A)
% s =
% 3×7 char array
% '0011000'
% '0011001'
% '0010000'
% if desired, convert to cell array of strings
c = cellstr(s)
% c =
% 3×1 cell array
% {'0011000'}
% {'0011001'}
% {'0010000'}
  2 件のコメント
Valentin Cayron
Valentin Cayron 2019 年 2 月 22 日
編集済み: Valentin Cayron 2019 年 2 月 22 日
Thanks a lot !
Is there anyway to say that one row like '0011000' is a binary string ?
Adam Danz
Adam Danz 2019 年 2 月 22 日
If you're acting on the cell array stored in 'c', the line below will produce a logical vector where 1s indicate rows that are binary (ie, composed of 1s and 0s). It does this by removing any 1s or 0s from each string and determining if that results in an empty string.
isBinary = cellfun(@(x)isempty(erase(x,{'0','1'})), c)

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

その他の回答 (1 件)

James Tursa
James Tursa 2019 年 2 月 22 日
result = char(A+'0');

カテゴリ

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