How to export matrix in the form of (column number, row number, value) in CSV?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a 100x100 matrix, and I want to export it to CSV in the form of (column number, row number, value). For example,
Matrix A:
[1] [2] [3] ... [100]
[1] 0.69 0.02 0.81 ... 0.55
[2] 0.23 0.67 0.02 ... 0.21
[3] 0.06 0.22 0.67 ... 0.96
[...] ... ... ... ... ...
[100] 0.01 0.05 0.22 ... 0.88
The expected CSV output is
Column Row Value
1 1 0.69
1 2 0.23
1 3 0.06
...
1 100 0.01
2 1 0.02
2 2 0.67
...
100 100 0.88
How can I do this in matlab? Your help is greatly appreciated.
0 comentarios
Respuesta aceptada
Voss
el 18 de En. de 2022
A = randn(8,4)
[m,n] = size(A);
[c,r] = meshgrid(1:n,1:m);
writematrix([c(:) r(:) A(:)],'output.csv');
% check the result
readmatrix('output.csv')
Más respuestas (0)
Ver también
Categorías
Más información sobre Multidimensional Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!