My first post, first question and eternal novice.

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

```
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:

```
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:

```
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.

Maybe a more julian way would be to use reduce:

```
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

```
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.

1 Like

Skoffer
November 9, 2020, 9:28pm
#3
Memory inefficient, but fun way to solve this task

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

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.

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.

1 Like

Skoffer
November 11, 2020, 10:45am
#6
If you will have any questions, do not hesitate to ask.

1 Like