Exporting and importing a 3D matrix

We generate a matrix like this

for i = 1:5
   for j = 1:5
      for k = 1:5
         d[i, j, k] = rand(1)
      end
   end
end

Is there an easy way to save matrix d somewhere and later on just call it and use it?
With

CSV.write("path/file_name.csv", Tables.table(d), header=false)

I can do it very easily for 2D matrix but I’m not sure if and how it works for a 3D matrix.

In this case probably GitHub - JuliaIO/HDF5.jl: Save and load data in the HDF5 file format from Julia is one of the options that you could consider.

There is also GitHub - JuliaIO/JLD2.jl: HDF5-compatible file format in pure Julia

Depending on your use case serialization might also be an option:

https://docs.julialang.org/en/v1/stdlib/Serialization/

Don’t call rand(1) here, just call rand(). The former returns a vector of length 1 while the latter returns just a number.

1 Like