# How to stream a large random matrix to a file?

**URL:** <https://discourse.julialang.org/t/how-to-stream-a-large-random-matrix-to-a-file/52673>\
**Category:** General Usage\
**Tags:** question, io\
**Created:** [December 31, 2020, 10:27pm UTC](https://discourse.julialang.org/t/how-to-stream-a-large-random-matrix-to-a-file/52673 "2020-12-31T22:27:58Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [January 1, 2021, 7:45pm UTC](https://discourse.julialang.org/t/how-to-stream-a-large-random-matrix-to-a-file/52673/4 "2021-01-01T19:45:58Z")

</div>

tldr: call open(… “w”) as in the [example provided by @stevengj here](https://discourse.julialang.org/t/storage-based-array-that-implements-views/52640/7).

Thanks to @oheil for [spotting additional bugs](https://discourse.julialang.org/t/how-to-stream-a-large-random-to-a-file/52673/2).

The following works:

```julia
using Test,Mmap

@testset "stream an m x n matrix to a file and read via Mmap" begin
    m=10
    n=10^5
    fn="/tmp/mmap.bin"
    s = open(fn, "w")
    a1 = rand(m)
    for i in 1:n
        i == 1 ? write(s,a1) : write(s, rand(m))
    end
    close(s)
    @test isfile(fn)

    s = open("/tmp/mmap.bin")
    A = Mmap.mmap(s, Matrix{Float64}, (m,n))
    @test A[:,1] == a1
end

Test Summary: | Pass Total
stream an m x n matrix to a file and read via Mmap | 2 2
Test.DefaultTestSet("stream an m x n matrix to a file and read via Mmap", Any[], 2, false)

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-stream-a-large-random-matrix-to-a-file/52673)._
