# How to display a matrix in 1 column?

조회 수: 1(최근 30일)
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.

로그인 to comment.

### 채택된 답변

Fangjun Jiang 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

Nurulhuda Ismail 4 Dec 2019
Thank you so much, it works. Can I ask one more think?
Is it possible to extract the elements that already in 1-column into text file?
fid = fopen('MatrixA.txt','wt');
for ii = 1:size(MatrixA,1)
fprintf(fid,'%g\t',MatrixA(ii,:));
fprintf(fid,'\n');
end
fclose(fid)
I used this code, however, it still extract in a matrix form, not the column form which is what I want.
Fangjun Jiang 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 4 Dec 2019
Thank you...you make my day for today..

로그인 to comment.