How to display a matrix in 1 column?

41 views (last 30 days)
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.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 4 Dec 2019
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 Comments
Fangjun Jiang
Fangjun Jiang on 4 Dec 2019
  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 on 4 Dec 2019
Thank you...you make my day for today..

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!