How to group elements of a logical array?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
I have a logical array of size 64x256. I want the result to be a cell array of size 64x16 where each cell has 16 bit data.I have attached an image of my data for reference.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/148255/image.jpeg)
For eg: Let T be the logical array
T= 0 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 0 0
0 0 0 0 0 0 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 1 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 0
0 0 0 0 0 0 1 1 0 1 1 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 1
0 0 0 0 0 0 1 0 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1
I want an output such that the cell data are grouped into 16 bit binary data as follows
T1='0000001011000010' '0000000101011100' '0000000111001100'
'0000001101111110' '0000000111000110' '0000001101000000'
'0000001110111111' '0000000011001101' '0000000110111010'
'0000001101101010' '0000001000011000' '0000001011101001'
'0000001011101011' '0000000001000011' '0000001010000101'
I tried reshape array but error message is shown.How to do it? Please help...Thanks in advance...
0 件のコメント
採用された回答
Stephen23
2015 年 3 月 24 日
編集済み: Stephen23
2015 年 3 月 24 日
T = [0,0,0,0,0,0,1,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,...
0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,0,0;...
0,0,0,0,0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,...
1,1,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0;...
0,0,0,0,0,0,1,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,...
1,1,0,0,1,1,0,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,1,0;...
0,0,0,0,0,0,1,1,0,1,1,0,1,0,1,0,0,0,0,0,0,0,1,0,...
0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,1;...
0,0,0,0,0,0,1,0,1,1,1,0,1,0,1,1,0,0,0,0,0,0,0,0,...
0,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1];
S = size(T);
X = mat2cell(T,ones(1,S(1)),16*ones(1,S(2)/16));
Y = cellfun(@(v)sprintf('%i',v), X, 'UniformOutput',false);
Which produces two useful cell arrays:
>> X
X =
[1x16 double] [1x16 double] [1x16 double]
[1x16 double] [1x16 double] [1x16 double]
[1x16 double] [1x16 double] [1x16 double]
[1x16 double] [1x16 double] [1x16 double]
[1x16 double] [1x16 double] [1x16 double]
>> Y
Y =
'0000001011000010' '0000000101011100' '0000000111001100'
'0000001101111110' '0000000111000110' '0000001101000000'
'0000001110111111' '0000000011001101' '0000000110111010'
'0000001101101010' '0000001000011000' '0000001011101001'
'0000001011101011' '0000000001000011' '0000001010000101'
0 件のコメント
その他の回答 (1 件)
Guillaume
2015 年 3 月 24 日
This may or may not be faster than Stephen's answer. Pick whichever you prefer:
T = [0,0,0,0,0,0,1,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,...
0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,0,0;...
0,0,0,0,0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,...
1,1,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0;...
0,0,0,0,0,0,1,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,...
1,1,0,0,1,1,0,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,1,0;...
0,0,0,0,0,0,1,1,0,1,1,0,1,0,1,0,0,0,0,0,0,0,1,0,...
0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,1;...
0,0,0,0,0,0,1,0,1,1,1,0,1,0,1,1,0,0,0,0,0,0,0,0,...
0,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1];
T1 = reshape(cellstr(char(reshape(T', 16, [])+'0')'), [], size(T, 1))'
outputs:
T1 =
'0000001011000010' '0000000101011100' '0000000111001100'
'0000001101111110' '0000000111000110' '0000001101000000'
'0000001110111111' '0000000011001101' '0000000110111010'
'0000001101101010' '0000001000011000' '0000001011101001'
'0000001011101011' '0000000001000011' '0000001010000101'
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!