# Is there a way to reshape a Vector to a row-major AbstractMatrix?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-reshape-a-vector-to-a-row-major-abstractmatrix/48214>\
**Category:** Performance\
**Created:** [October 12, 2020, 11:45am UTC](https://discourse.julialang.org/t/is-there-a-way-to-reshape-a-vector-to-a-row-major-abstractmatrix/48214 "2020-10-12T11:45:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrojhgu](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@astrojhgu](https://discourse.julialang.org/u/astrojhgu)\
**Post date:** [October 12, 2020, 11:45am UTC](https://discourse.julialang.org/t/is-there-a-way-to-reshape-a-vector-to-a-row-major-abstractmatrix/48214/1 "2020-10-12T11:45:37Z")

</div>

I have a 1-D vector of size of (n_m, 1), i.e., length=n_m. I need to reshape it to a n rows m cols matrix, like

```julia
1 5 9
2 6 10
3 7 11
4 8 12

```

, but in memory it is row-majored. So that I can access its rows efficiently.

Currently my solution is

```julia
a=collect(1:12) # say a is a length=12 Vector, the value is not important here
reshaped=transpose(collect(transform(reshape(a, 4, 3))))

```

Then `reshaped` is a 4x3 AbstractMatrix and row-majored. Is there any former or better way to do this?  
Thanks.

I also tried parallel assigning-based method, which is faster, but requires multithreading.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 12, 2020, 12:20pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-reshape-a-vector-to-a-row-major-abstractmatrix/48214/2 "2020-10-12T12:20:26Z")

</div>

I think there is no row majored array type available. This may be a starting point to read about:  
[https://github.com/JuliaLang/julia/issues/20311](https://github.com/JuliaLang/julia/issues/20311)  
which links also to  
[https://github.com/JuliaArrays](https://github.com/JuliaArrays)

For your solution; I don’t know where transform comes from but it seems that this is enough:

```julia
reshaped=transpose(reshape(a, 4, 3))

```

~~which again is a column major matrix.~~ Which is indeed a row major matrix.  
If you just need to go row wise through your matrix, you may try  
`eachrow`  
for its performance.E.g:

```julia
julia> collect(eachrow(a))
4-element Array{SubArray{Int64,1,Array{Int64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int64}}},true},1}:
 [1, 5, 9]
 [2, 6, 10]
 [3, 7, 11]
 [4, 8, 12]

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 12, 2020, 2:46pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-reshape-a-vector-to-a-row-major-abstractmatrix/48214/3 "2020-10-12T14:46:59Z")

</div>

Actually

```julia
reshaped = transpose(reshape(a, 4, 3))

```

is a row-major matrix, not column-major, meaning that it is faster to iterate along the rows than along the columns. `transpose` creates a wrapper that basically switches the order of the indices.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 12, 2020, 3:05pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-reshape-a-vector-to-a-row-major-abstractmatrix/48214/5 "2020-10-12T15:05:38Z")

</div>

You are right, I didn’t know that.  
And as seeing is believing I had to check it out:

```julia
using BenchmarkTools

a=rand(Float64,10000,1000)
ta=transpose(a)

function sum_over_col(a)
	s=0.0
	for c in axes(a,2)
		for r in axes(a,1)
			s+=a[r,c]
		end
	end
	s
end

function sum_over_row(a)
	s=0.0
	for r in axes(a,1)
		for c in axes(a,2)
			s+=a[r,c]
		end
	end
	s
end
 
@benchmark sum_over_col($a)
@benchmark sum_over_row($a)
@benchmark sum_over_col($ta)
@benchmark sum_over_row($ta)

```

Yields:

```julia
julia> @benchmark sum_over_col($a)
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 9.441 ms (0.00% GC)
  median time: 9.781 ms (0.00% GC)
  mean time: 9.887 ms (0.00% GC)
  maximum time: 14.099 ms (0.00% GC)
  --------------
  samples: 506
  evals/sample: 1

julia> @benchmark sum_over_row($a)
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 33.806 ms (0.00% GC)
  median time: 34.650 ms (0.00% GC)
  mean time: 35.018 ms (0.00% GC)
  maximum time: 40.732 ms (0.00% GC)
  --------------
  samples: 143
  evals/sample: 1

julia> @benchmark sum_over_col($ta)
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 33.866 ms (0.00% GC)
  median time: 34.664 ms (0.00% GC)
  mean time: 35.040 ms (0.00% GC)
  maximum time: 41.581 ms (0.00% GC)
  --------------
  samples: 143
  evals/sample: 1

julia> @benchmark sum_over_row($ta)
BenchmarkTools.Trial:
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 9.403 ms (0.00% GC)
  median time: 9.810 ms (0.00% GC)
  mean time: 9.961 ms (0.00% GC)
  maximum time: 11.895 ms (0.00% GC)
  --------------
  samples: 502
  evals/sample: 1

```

Nice. Is there some reference on the How’s and Why’s?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 12, 2020, 3:13pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-reshape-a-vector-to-a-row-major-abstractmatrix/48214/6 "2020-10-12T15:13:52Z")

</div>

Not sure about the hows, but the whys are that you want fast calculations involving transposes. You can create a transposed matrix lazily without allocating any new array, and multiplications involving transposed matrices often involves iteration along rows, so it’s a important that this is fast.
