# Sequential multiple hcat()s of a range of rows?

**URL:** <https://discourse.julialang.org/t/sequential-multiple-hcat-s-of-a-range-of-rows/49852>\
**Category:** New to Julia\
**Created:** [November 9, 2020, 4:48pm UTC](https://discourse.julialang.org/t/sequential-multiple-hcat-s-of-a-range-of-rows/49852 "2020-11-09T16:48:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![forestacorn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forestacorn/32/19260_2.png) [@forestacorn](https://discourse.julialang.org/u/forestacorn)\
**Post date:** [November 9, 2020, 4:48pm UTC](https://discourse.julialang.org/t/sequential-multiple-hcat-s-of-a-range-of-rows/49852/1 "2020-11-09T16:48:58Z")

</div>

My first post, first question and eternal novice.

I wish to sequentially horizontal concatenate a range of rows from an array:

```julia
A = [
	0 1
	2 3
	4 5
	6 7
	8 9
	]

```

So far this is the function I have come up with to perform the operation:

```julia
function multihcat(A, steps)
	B = A[1:end-steps, :]
	for i ∈ 1:steps
		B = hcat(B, A[1+i:end-(steps-i),:])
	end
	return B
end

```

Then running `multihcat(A, 2)` gives the desired result:

```julia
3×6 Array{Int64,2}:
 0 1 2 3 4 5
 2 3 4 5 6 7
 4 5 6 7 8 9

```

However I think I am missing a much more elegant Julia way of doing this and welcome suggestions of better solutions.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 9, 2020, 9:00pm UTC](https://discourse.julialang.org/t/sequential-multiple-hcat-s-of-a-range-of-rows/49852/2 "2020-11-09T21:00:40Z")

</div>

Maybe a more julian way would be to use reduce:

```julia
 multihcat(A, steps) = reduce(hcat, A[i+1:end-(steps-i), :] for i in 0:steps)

```

However, since the size of B is known in advance, it makes the most sense to preallocate it with

```julia
n, m = size(A)
B = similar(A, n-steps, m*(steps+1)

```

And then fill it in a loop. This will be the fastest and most memory efficient.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [November 9, 2020, 9:28pm UTC](https://discourse.julialang.org/t/sequential-multiple-hcat-s-of-a-range-of-rows/49852/3 "2020-11-09T21:28:27Z")

</div>

Memory inefficient, but fun way to solve this task

```julia
hcat(circshift.(Ref(A), 0:-1:-2)...)[1:3, :]

```

---

<div class="post-metadata">

**Author:** ![forestacorn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forestacorn/32/19260_2.png) [@forestacorn](https://discourse.julialang.org/u/forestacorn)\
**Post date:** [November 11, 2020, 10:23am UTC](https://discourse.julialang.org/t/sequential-multiple-hcat-s-of-a-range-of-rows/49852/4 "2020-11-11T10:23:42Z")

</div>

Thank you for the feedback. I agree, preallocating the array, because its size is known in advance, would be a better approach, I will try that.

---

<div class="post-metadata">

**Author:** ![forestacorn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forestacorn/32/19260_2.png) [@forestacorn](https://discourse.julialang.org/u/forestacorn)\
**Post date:** [November 11, 2020, 10:28am UTC](https://discourse.julialang.org/t/sequential-multiple-hcat-s-of-a-range-of-rows/49852/5 "2020-11-11T10:28:34Z")

</div>

Thank you for the suggestion, it’s very compact and I will have to study it for a while to understand it. I’ve never used `circshift` before but looks like it can be useful.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [November 11, 2020, 10:45am UTC](https://discourse.julialang.org/t/sequential-multiple-hcat-s-of-a-range-of-rows/49852/6 "2020-11-11T10:45:47Z")

</div>

If you will have any questions, do not hesitate to ask.
