# Resize!(matrix)

**URL:** <https://discourse.julialang.org/t/resize-matrix/2379>\
**Category:** General Usage\
**Tags:** question, matrix\
**Created:** [March 1, 2017, 12:35pm UTC](https://discourse.julialang.org/t/resize-matrix/2379 "2017-03-01T12:35:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 1, 2017, 12:35pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/1 "2017-03-01T12:35:20Z")

</div>

is it possible to resize an `Array` with more than one dimension?

For example:

```julia-auto
tmp1 = collect(1:6)
@show pointer(tmp1)

for i in 1:5
    resize!(tmp1, length(tmp1) + 2)
    @show pointer(tmp1)

    tmp2 = reshape(tmp1, 2, Int(length(tmp1) / 2))
    @show pointer(tmp2)

    # do something that requires tmp2 as a matrix
    # ...
end

```

gives me an `cannot resize array with shared data` error

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 2, 2017, 4:02pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/2 "2017-03-02T16:02:22Z")

</div>

There is not. You have to create a new array and copy data to it, which is what any implementation of this functionality would have to do under the hood. There’s been a few discussions of supporting this, but the fact that it’s inherently so expensive has always steered the decision in favor of making the fact that it’s an expensive operation explicit. Another way to do this is to use a different data structure: a vector of vectors. For that data structure, adding or removing “columns” is cheap.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 2, 2017, 4:34pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/3 "2017-03-02T16:34:22Z")

</div>

the problem is that a vector of vectors does not have a contiguous memory layout any more.

I solved it exactly the way you described in my special case the size of the matrix does not change very often, so it is not a real problem for me.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 20, 2018, 2:54pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/5 "2018-04-20T14:54:58Z")

</div>

If I only need to resize the number of columns, and am OK requiring `A = unsafe_resize!(A, m)`, is the following “safe”?

```julia
function unsafe_resize!(W::Matrix, m::Integer)
    if m == size(W,2)
        W
    else
        n=size(W,1)
        reshape(resize!(vec(W),n*m),n,m)
    end
end

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 20, 2018, 2:58pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/6 "2018-04-20T14:58:28Z")

</div>

I’m pretty sure it’s not. If the original allocated array is not 1-dimensional, then I think the runtime assumes that it cannot be resized or moved. It would be possible to lift that restriction but it would be considerable work to make sure that’s not being relied on anywhere and that we don’t get performance regressions due to it no longer being guaranteed that a multidimensional array’s bounds won’t change.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [April 20, 2018, 3:00pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/7 "2018-04-20T15:00:55Z")

</div>

As long as you just need to resize the last dimension, [ElasticArrays.jl](https://github.com/JuliaArrays/ElasticArrays.jl) might work for you.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 20, 2018, 3:01pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/8 "2018-04-20T15:01:44Z")

</div>

Ah OK, so I guess best to wrap a `Vector` with a custom matrix type.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [April 20, 2018, 3:13pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/9 "2018-04-20T15:13:31Z")

</div>

> [@gdkrmr](#):
>
> the problem is that a vector of vectors

Shameless advertising: I’m preparing a package for this use case: [ArraysOfArrays.jl](https://github.com/oschulz/ArraysOfArrays.jl/tree/dev/src). Most of the coding is done, I hope to have it ready for registration soon (still needs test code and some docs). You’re welcome to give the “dev” branch a try, I’d be happy to get some early feedback. ArraysOfArrays provides two types:

- `ArrayOfSimilarArrays`: Provides things like a vector of vectors (all of same length), backed by a single multidimensional array. When backed by an ElasticArray, it supports `resize!`, etc. For the case of vector-of-vectors, there’s an alias `VectorOfSimilarVectors`.

- `VectorOfArrays`: A vector of arrays (of same number of dinensions, but different size), backed by a single vector. When backed by a standard `Base.Vector` (the default) it supports `push!`, etc. For the case vector-of-vectors, there’s an alias `VectorOfVectors`.

ArraysOfArrays.jl will support `UnsafeArrays.@uview`.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 20, 2018, 3:15pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/10 "2018-04-20T15:15:46Z")

</div>

Cool! FYI, this is along the same lines as `PseudoBlockArray` in BlockArrays.jl, though their use cases is slightly different.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [April 20, 2018, 3:22pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/11 "2018-04-20T15:22:48Z")

</div>

> [@dlfivefifty](#):
>
> Cool! FYI, this is along the same lines as PseudoBlockArray in BlockArrays.jl, though their use cases is slightly different.

With `PseudoBlockArray`, the blocks have the same number of dimensions as the parent, though, right?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 20, 2018, 3:23pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/12 "2018-04-20T15:23:44Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [April 20, 2018, 3:25pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/13 "2018-04-20T15:25:25Z")

</div>

Thanks for the hint, though, I knew BlockArrays.jl, but I missed PseudoBlockArray - it may come in very handy for some other things I need to do with arrays!

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [April 20, 2018, 3:28pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/14 "2018-04-20T15:28:08Z")

</div>

I’m curious - why does PseudoBlockArray use copies instead of views on `getindex`?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 20, 2018, 3:44pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/15 "2018-04-20T15:44:14Z")

</div>

I don’t understand the question: calling `getindex` should call `getindex`…

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [April 20, 2018, 3:46pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/16 "2018-04-20T15:46:40Z")

</div>

Oops, sorry - I meant `getblock`, not `getindex`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 20, 2018, 3:46pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/17 "2018-04-20T15:46:50Z")

</div>

> [@oschulz](#):
>
> Shameless advertising: I’m preparing a package for this use case: ArraysOfArrays.jl . Most of the coding is done, I hope to have it ready for registration soon (still needs test code and some docs). You’re welcome to give the “dev” branch a try, I’d be happy to get some early feedback. ArraysOfArrays provides two types:
> 
> ArrayOfSimilarArrays: Provides things like a vector of vectors (all of same length), backed by a single multidimensional array. When backed by an ElasticArray, it supports resize!, etc. For the case of vector-of-vectors, there’s an alias VectorOfSimilarVectors.
> 
> VectorOfArrays: A vector of arrays (of same number of dinensions, but different size), backed by a single vector. When backed by a standard Base.Vector (the default) it supports push!, etc. For the case vector-of-vectors, there’s an alias VectorOfVectors.
> 
> ArraysOfArrays.jl will support UnsafeArrays.@uview.

How does this differ from [RecursiveArrayTools.jl](https://github.com/JuliaDiffEq/RecursiveArrayTools.jl)? Anything we can/should take up? It’s built specifically for the purpose of growing arrays and tensors one “column” at a time for DiffEq adaptive solutions but `VectorOfArrays` has been more generally useful.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [April 20, 2018, 3:47pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/18 "2018-04-20T15:47:17Z")

</div>

This is a very common operation to perform in high-performance computing. A package providing a matrix type with this functionality would be more than welcome. I asked the same question on SO some days ago.

EDIT: it seems ElasticArrays.jl does the job for resizing the matrix in the last dimension (columns).

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [April 20, 2018, 3:47pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/19 "2018-04-20T15:47:37Z")

</div>

That’s probably an anachronism: use `view(A, Block(1,2))` if you want a view of a block.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [April 20, 2018, 3:47pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/20 "2018-04-20T15:47:50Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [April 20, 2018, 3:52pm UTC](https://discourse.julialang.org/t/resize-matrix/2379/21 "2018-04-20T15:52:58Z")

</div>

> [@ChrisRackauckas](#):
>
> How does this differ from RecursiveArrayTools.jl? Anything we can/should take up?

From what I understand (please correct me!), `RecursiveArrayTools.VectorOfArray` has the underlying data structure `Vector{Array}`, while `ArraysOfArrays.VectorOfArrays` is backed by a single, contiguous array. It’s data structure is more similar to `SparseArray`, internally.

[Next page](https://discourse.julialang.org/t/resize-matrix/2379.md?page=2)
