# Suggestions for manipulating an array

**URL:** https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946
**Category:** General Usage
**Created:** [September 19, 2019, 1:44pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946 "2019-09-19T13:44:51Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 19, 2019, 1:44pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946/1 "2019-09-19T13:44:51Z")

</div>

Hey guys and girls

I have this case, where I have a row vector N, which I want to rewrite to a matrix of these forms:

```julia
N = [N1 N2 N3 N4]

N2D = [N1 0 N2 0 N3 0 N4 0;
       0 N1 0 N2 0 N3 0 N4];

N3D = [N1 0 0 N2 0 0 N3 0 0 N4 0 0; 0 N1 0 0 N2 0 0 N3 0 0 N4 0 ; 0 0 N1 0 0 N2 0 0 N3 0 0 N4]

```

Currently I am doing it like this:

```julia
Nx = 8
N2D = Array{Basic}(undef, 2, Nx)
N2D[1,2:2:Nx] = [0 0 0 0]
N2D[2,1:2:Nx-1] = [0 0 0 0]
N2D[1,1:2:Nx-1] = N
N2D[2,2:2:Nx] = N

```

In the past I hardcoded a for loop, but maybe there is a way to make a function do this transformation more effeciently.

Kind regards

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [September 19, 2019, 2:19pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946/2 "2019-09-19T14:19:32Z")

</div>

A `for` loop should be extremely efficient. If that solution is already working for you, then you’re already done!

Is constructing this matrix actually a performance bottleneck? If so, have you considered not constructing it at all? For example, you could create your own `AbstractArray` which stores only the non-zero entries and implement `Base.getindex(::MyNewArrayType, i)` to return the right nonzero entry or zero as appropriate. Or, for that matter, can you just use `SparseArrays`, which already do exactly that?

A lot of these questions will be easier to answer if you can tell us more about what your performance bottlenecks are and what you’re planning to do with the resulting data structure.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [September 19, 2019, 2:44pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946/3 "2019-09-19T14:44:14Z")

</div>

May be you can use this function? I’m not sure what your real situation is though, more information about the context can help.

```julia
julia> function build_nD(N,dim)
           out = fill(0,dim,dim*length(N))
           for i = 1:dim
               for j = 1:length(N)
                   out[i,(j-1)dim+i] = N[j]
               end
           end
           out
       end
build_nD (generic function with 1 method)

julia> N = [1 2 3 4];

julia> dim = 5;

julia> build_nD(N,dim)
5×20 Array{Int64,2}:
 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0
 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0
 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0
 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0
 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 19, 2019, 2:55pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946/4 "2019-09-19T14:55:03Z")

</div>

Hey again, so it isn’t really a performance bottleneck for me, I just enjoy seeing/learning how others will approach a similar problem - sometimes I have experienced that I make a complex and working solution and then some one pops in, and says you can just do this, this and that. Always fun for me to learn more - I will have a look at sparse arrays and trying to define something as you describe.

Kind regards

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 19, 2019, 2:55pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946/5 "2019-09-19T14:55:47Z")

</div>

Thanks, seems close to what I want and much smaller than my own solution - will try to test it later and let you hear back.

Kind regards

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 19, 2019, 3:10pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946/6 "2019-09-19T15:10:52Z")

</div>

> [@Seif\_Shebl](#):
>
> function build\_nD(N,dim) out = fill(0,dim,dim\*length(N)) for i = 1:dim for j = 1:length(N) out[i,(j-1)dim+i] = N[j] end end out end

Thank you very much for your code bit - I modified it slightly, so that it also determines and preallocates the right type:

```julia
function build_nD(N,dim)
    typ = eltype(N)
           out = Array{typ,2}(fill(0,dim,dim*length(N)))
           for i = 1:dim
               for j = 1:length(N)
                   out[i,(j-1)dim+i] = N[j]
               end
           end
           out
       end

```

This was necessary since I was using SymEngine and symbolic variables cannot be inserted into int/float arrays. It was much more compact than my first for loop, so I am pretty happy with it.

Kind regards

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [September 19, 2019, 3:20pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946/7 "2019-09-19T15:20:00Z")

</div>

> [@Ahmed\_Salih](#):
>
> typ = eltype(N) out = Array{typ,2}(fill(0,dim,dim\*length(N)))

May be this is more readable, you don’t need to specify `Array{T,2}`:

```julia
T = eltype(N)
out = zeros(T, dim,dim*length(N))

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 19, 2019, 4:56pm UTC](https://discourse.julialang.org/t/suggestions-for-manipulating-an-array/28946/8 "2019-09-19T16:56:16Z")

</div>

> [@Seif\_Shebl](#):
>
> zeros(T, dim,dim\*length(N))

Thank! I used that change.
