Using fprintf for matrix

630 ビュー (過去 30 日間)
Ali
Ali 2016 年 5 月 30 日
回答済み: Ambrish Raghav 2020 年 7 月 30 日
I have a matrix with 300 rows and 2 columns. For instance:
1 2
2 4
5 6
and another matrix with 300 rows and 1 column: for instance:
1
3
4
I want to convert them in the following:
-1: 1 2
-3: 2 4
-4: 5 6
It would be appreciated if you could help me. Thanks

採用された回答

Stephen23
Stephen23 2016 年 5 月 30 日
編集済み: Stephen23 2016 年 5 月 30 日
>> X = [1,2;2,4;5,6];
>> Y = [1;3;4];
>> fprintf('-%d: %d %d\n', [Y,X].')
-1: 1 2
-3: 2 4
-4: 5 6
It is worth reading the fprintf documentation carefully, to see how it handles matrices (columnwise!) and to pick the best format string for your requirements.
  1 件のコメント
Ali
Ali 2016 年 5 月 30 日
Thank you very much for the help.

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

その他の回答 (1 件)

Ambrish Raghav
Ambrish Raghav 2020 年 7 月 30 日
X = [1,2;2,4;5,6];
>> Y = [1;3;4];
>> fprintf('-%d: %d %d\n', [Y,X].')
-1: 1 2
-3: 2 4
-4: 5 6

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by