How to concatenate each row of a matrix into a vector ?
8 ビュー (過去 30 日間)
古いコメントを表示
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 ?
0 件のコメント
採用された回答
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 件のコメント
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 件)
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!