want to print a matrix with in bits form

2 次查看(过去 30 天)
S0=[01 00 11 10;11 10 01 00;00 10 01 11;11 01 11 10] this matrix is appeared like this
S0 = 1 0 11 10
11 10 1 0
0 10 1 11
11 1 11 10
but i want to print this matrix like this
S0 =
01 00 11 10
11 10 01 00
00 10 01 11
11 01 11 10
  2 个评论
James Tursa
James Tursa 2017-2-22
Do you mean you want MATLAB to automatically put leading 0's in front of double values in a matrix when it displays the matrix? Or are you just asking for a way to print it like that manually with some code or function?
ablaze
ablaze 2017-2-27
编辑:ablaze 2017-2-27
actually want to represent binary(in bits). is there any method to represent binary vlaues ????

请先登录,再进行评论。

回答(2 个)

James Tursa
James Tursa 2017-2-22
If you are just trying to manually print the matrix in that format, e.g.,
for k=1:size(S0,1)
fprintf(' %02d',S0(k,:));
fprintf('\n')
end

Walter Roberson
Walter Roberson 2017-2-27
S0=[01 00 11 10;11 10 01 00;00 10 01 11;11 01 11 10];
S1 = arrayfun( @(v) [floor(v/10), mod(v,10)], S0, 'Uniform', 0);
This will create a 4 x 4 array of cells, and each cell will be a 1 x 2 double which is the binary.
Binary adds a dimension to your existing 4 x 4 array and it is not clear how you would like that dimension represented.
Perhaps you would like
S1 = arrayfun( @(v) char('0' + [floor(v/10), mod(v,10)]), S0, 'Uniform', 0);
but if so then the simpler method would be
S1 = arrayfun(@(v) sprintf('%02d', v), S0, 'Uniform', 0);

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by