fprintf matrix same row and column
2 次查看(过去 30 天)
显示 更早的评论
Hi I have a 8x12 matrix with values 0s and 1s inside. How do i fprintf this matrix into the command window whilst keeping each element of the matrix in the same row and column? Thanks
0 个评论
回答(2 个)
Star Strider
2016-5-7
See if this does what you want:
m = randi([0 1], 8, 12);
fprintf([repmat('\t%.0f', 1, 12) '\n'], m')
2 个评论
Stephen23
2016-5-7
编辑:Stephen23
2016-5-7
@alexander li: Did you even try this code? It does exactly what you want.
It doesn't matter what values are in the matrix. Look at the code: the values of the matrix m are not used anywhere, so why would they matter? Star Strider simply used a random matrix to test the code (because you did not give any example matrix). It is common to test code using random numbers, because they are easy to generate and they show that there is nothing "magic" about the values used.
Stephen23
2016-5-7
As an alternative to fprintf, disp also does the trick:
>> m = randi([0 1], 8, 12);
>> disp(m)
1 1 0 1 0 0 1 1 1 1 0 0
1 1 1 1 0 0 1 0 1 0 1 1
0 0 1 1 0 1 0 1 1 1 1 0
1 1 1 0 1 1 1 0 0 0 1 0
1 1 1 1 1 0 1 1 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 1
0 1 1 1 1 0 0 1 1 1 1 1
1 0 1 0 0 1 0 1 0 0 1 0
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!