Using fprintf for matrix

조회 수: 182 (최근 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 CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by