How to export matrix in the form of (column number, row number, value) in CSV?
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 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')
More Answers (0)
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!