adding index to matrix rows
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ali
am 27 Mai 2016
Kommentiert: Allison Chua
am 6 Okt. 2020
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
0 Kommentare
Akzeptierte Antwort
Guillaume
am 27 Mai 2016
Bearbeitet: Stephen23
am 27 Mai 2016
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 Kommentare
Guillaume
am 27 Mai 2016
Well simply modify the relevant fprintf:
fprintf('%d:', row - 1);
Weitere Antworten (1)
Renato Agurto
am 27 Mai 2016
Bearbeitet: Renato Agurto
am 27 Mai 2016
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 Kommentare
Allison Chua
am 6 Okt. 2020
@Renato Agurto - thank you SO much!!! I'm so terrible at Matlab syntax, and this was exactly what I needed.
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!