How to fprintf a transposed matrix?

Hello, I am trying to use fprintf to show the values of a transposed matrix. However, it does not seem to show the values as I would hope.
A = [7 11; 15 23; 9 7];
T=A.';
fprintf('\n A''= \n');
fprintf(' %d \t %d \t %d\n',T);
The result will be;
7 11 15
23 9 7
But I am expecting;
7 15 9
11 23 7

回答 (2 件)

Stephen23
Stephen23 2022 年 2 月 16 日
編集済み: Stephen23 2022 年 2 月 16 日

2 投票

fprintf(..,T.');
% ^^
Or skip T entirely:
fprintf(..,A);
In MATLAB, just like in many other mathematical computation languages (e.g. FORTRAN), array elements are stored going down the rows first, then along the columns, etc.
When you process data in linear order (e.g. as FPRINTF does), you always need to take into account the order that the data are actually stored in memory.

1 件のコメント

Anthony Sullivan
Anthony Sullivan 2022 年 2 月 16 日
Thank you! Didn't realize the original transpose wouldn't carry over

サインインしてコメントする。

Jan
Jan 2022 年 2 月 16 日

0 投票

fprintf takes the argument elementwise as they are store in the memory. So:
A = [7 11; 15 23; 9 7];
fprintf(' %d \t %d \t %d\n', T);
is equivalent to:
fprintf(' %d \t %d \t %d\n', T(:));
Transposing the matrix is the solution, as Stephen has answered already.

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 2 月 16 日

コメント済み:

2022 年 2 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by