How to display a matrix in 1 column?

조회 수: 36 (최근 30일)
Nurulhuda Ismail
Nurulhuda Ismail 2019년 12월 4일
댓글: Nurulhuda Ismail 2019년 12월 4일
If I have a 3x3 matrix, then I want to display all the elements in 1-column. What I should do is I use a code written like this:
MatrixA =complex(rand(3),rand(3));
yourvector = MatrixA(:);
And the output should be like this:
MatrixA =
0.0368 + 0.8469i 0.4985 + 0.9399i 0.2231 + 0.9552i
0.1905 + 0.4090i 0.4067 + 0.9367i 0.1639 + 0.6461i
0.1353 + 0.6872i 0.2031 + 0.5439i 0.8975 + 0.6536i
yourvector =
0.0368 + 0.8469i
0.1905 + 0.4090i
0.1353 + 0.6872i
0.4985 + 0.9399i
0.4067 + 0.9367i
0.2031 + 0.5439i
0.2231 + 0.9552i
0.1639 + 0.6461i
0.8975 + 0.6536i
However, how am I going to separate the column based on the row?I expect my result should be like this:
0.0368 + 0.8469i
0.1905 + 0.4090i
0.1353 + 0.6872i
0.4985 + 0.9399i
0.4067 + 0.9367i
0.2031 + 0.5439i
0.2231 + 0.9552i
0.1639 + 0.6461i
0.8975 + 0.6536i
Can someone help me? Thank you.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2019년 12월 4일
Don't struggle too much. A for-loop will do. Or displaying "column #" would be more helpful when the array size is bigger.
for k=1:size(a,2)
disp(a(:,k));
disp(' ');
end
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2019년 12월 4일
  1. Notice the difference between "\t" and "\n" in fprintf() in your code
  2. fprintf() won't print complex numbers
  3. Using diary() might give you what you want
diary('MatrixA.txt');
for k=1:size(a,2)
disp(a(:,k));
disp(' ');
end
diary off;
Nurulhuda Ismail
Nurulhuda Ismail 2019년 12월 4일
Thank you...you make my day for today..

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by