Help reshaping a 3D array

Hello! I want to make a 3D array. From this I will group three different data vectors to put in this 3-D array. However, I’m having problem with the reshape function.

Img = reshape([[KrogagerR],[KrogagerG],[KrogagerB]], (lin, col, 3))
but, this giving the error: DimensionMismatch(“new dimensions (7075, 3300, 3) must be consistent with array size 3”)

When you write x = [r, g, b], you’re actually creating a 3-element array of arrays. In other words, accessing x[1] is going to give you the entire r vector. When you write x = [[r], [g], [b]], you’re further nesting each vector inside an additional one-element array.

What you probably want is to concatenate the arguments together into one flattened structure. A great way to do this is with the cat function — with it you can specify which dimension you want things to concatenate across. In this case, you can do cat(r, g, b, dims=3) — which will “just work” if your individual components are already matrices in the shape of your image. If they’re vectors, well, then you’ll end up with an Nx1x3 array, which you can then reshape into the dimensions you’re after.

(I split this out from the other thread as it’s an unrelated question)

4 Likes

Thank @mbauman. Your idea worked.