I have a matrix like this
1 0
0 2
2 1
0 3
3 2
2 0
4 1
1 6
6 4
6 1
I would like to have the following matrix, please help me if you have the answer.
0: 1 0
1: 0 2
2: 2 1
3: 0 3
4: 3 2
5: 2 0
6: 4 1
7: 1 6
8: 6 4
9: 6 1

 채택된 답변

Guillaume
Guillaume 2016년 5월 27일
편집: Stephen23 2016년 5월 27일

0 개 추천

Please use the code format button {}Code rather than putting spaces between each line.
A matrix can only contain numbers, colons are not allowed. If it's only for display that you want that, there's no simple way to force matlab to display matrices like this.
Possibly, you can convert the matrix into a table and assign names to the rows:
m = [1 0;0 2; 2 1; 0 3; 3 2; 2 0; 4 1];
t = array2table(m, 'RowNames', sprintfc('%d:', 1:size(m, 1)), 'VariableNames', sprintfc('col%d', 1:size(m, 2)))
Otherwise, you'll have to write your own display function which is far from trivial if you want it to play nicely with format.

댓글 수: 6

Thank you very much Guillaume and Stephen Cobeldick. The code is working well, but I do not want first two line. I mean I do not want col1 col2 and line in the bottom of them.
col1 col2
____ ____
1: 0 2
2: 2 4
3: 4 0
4: 5 0
5: 0 4
6: 4 5
7: 3 1
8: 1 6
9: 6 3
Unfortunately, you do not have a choice with tables. The header is always printed.
You will have to come up with your own display function. The following will work as long as all numbers are integers and none of the numbers have more than 8 digits:
function displayrownumber(m)
validateattributes(m, {'numeric'}, {'integer', '2d', 'nonsparse'});
for row = 1 : size(m, 1)
fprintf('%d:', row);
fprintf('% 9d', m(row, :));
fprintf('\n');
end
end
A completely generic function that works with non-integer is not something I'm willing to spend time on, particularly as I don't see the point of the display. You can just open the matrix with the variable editor if you want to see row numbers.
Ali
Ali 2016년 5월 27일
Dear Guillaume ,
you really help me here, and I am thankful. The thing is the appearance is important for me because I am writing a code to convert a text file to another. thanks
Ali
Ali 2016년 5월 27일
I used Guillaume code, but I still have a problem here. I would like to first line start with zero and not one. the code gives me this:
1: 1 0
2: 0 2
3: 2 1
4: 0 3
5: 3 2
but I want this:
0: 1 0
1: 0 2
2: 2 1
3: 0 3
4: 3 2
Well simply modify the relevant fprintf:
fprintf('%d:', row - 1);
Ali
Ali 2016년 5월 27일
Many thanks Guillaume. you helped me a lot

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Renato Agurto
Renato Agurto 2016년 5월 27일
편집: Renato Agurto 2016년 5월 27일

2 개 추천

Is this what you want? Or what do you mean with ':' in a matrix?
A = [1 0;
0 2;
2 1;
0 3;
3 2;
2 0;
4 1;
1 6;
6 4;
6 1];
B = [(0:size(A,1)-1)' A];

댓글 수: 2

Ali
Ali 2016년 5월 27일
Many thanks for the response. It is appreciated. I have a matrix with 500 row and 2 columns. I would like to be converted in this format: for instance, this is part of the results that I am looking for
0: 1 0
1: 0 2
2: 2 1
3: 0 3
4: 3 2
5: 2 0
6: 4 1
Allison Chua
Allison Chua 2020년 10월 6일
@Renato Agurto - thank you SO much!!! I'm so terrible at Matlab syntax, and this was exactly what I needed.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

Ali
2016년 5월 27일

댓글:

2020년 10월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by