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

 Réponse acceptée

Stephen23
Stephen23 le 30 Mai 2016
Modifié(e) : Stephen23 le 30 Mai 2016

7 votes

>> 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 commentaires

Ali
Ali le 30 Mai 2016
Thank you very much for the help.
Chance
Chance le 4 Mar 2026 à 6:17
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Question posée :

Ali
le 30 Mai 2016

Commenté :

le 4 Mar 2026 à 6:17

Community Treasure Hunt

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

Start Hunting!

Translated by