How to export matrix in the form of (column number, row number, value) in CSV?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Voss
le 18 Jan 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')
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!