Fastest way to export 2d matrix to CSV file with triplets in Matlab
2 answers
Assuming which A
is the input matrix, two approaches can be suggested here.
fprintf
-
output_file = 'data.txt'; %// Edit if needed to be saved to a different path
At = A.'; %//'
[y,x] = ndgrid(1:size(At,1),1:size(At,2));
fid = fopen(output_file, 'w+');
for ii=1:numel(At)
fprintf(fid, '%d,%d,%d\n',x(ii),y(ii),At(ii));
end
fclose(fid);
dlmwrite
-
At = A.'; %//'
[y,x] = ndgrid(1:size(At,1),1:size(At,2));
dlmwrite(output_file,[x(:) y(:) At(:)]);
Some quick tests show fprintf
it performs better on various inputs.
+2
source to share