fprintf matrix same row and column

조회 수: 2 (최근 30일)
alexander li
alexander li 2016년 5월 7일
편집: Stephen23 2016년 5월 7일
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

답변 (2개)

Star Strider
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
alexander li
alexander li 2016년 5월 7일
The matrix i have is some data which i have received so it's not random numbers. I want to print that data. Thanks
Stephen23
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
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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by