Using fprintf for matrix

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.

2 个评论

Thank you very much for the help.
Thank you so much, I was wondering why my code was beig weird and i forgot to put the .' at the end of the brackets. I was struggling to fix this for so long and yoou helped me very much thank you.

请先登录,再进行评论。

更多回答(1 个)

Ambrish Raghav
Ambrish Raghav 2020-7-30

0 个投票

此 个回答 已被 Dyuman Joshi 标记
  • Dyuman Joshi 在 21 minutes 前 标记。

    Copy of Stephen23's answer

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

类别

帮助中心File Exchange 中查找有关 Operators and Elementary Operations 的更多信息

提问:

Ali
2016-5-30

评论:

about 23 hours 前

Community Treasure Hunt

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

Start Hunting!

Translated by