logankilpatrick
logankilpatrick

Reputation: 14521

How to convert a 4D array to two 3D arrays in Julia?

I have an array which looks like:

224×224×3×2 Array{Float32, 4}:
[:, :, 1, 1] =
 0.117647   0.117647   0.117647   0.117647   …  0.384314  0.396078  0.403922
 0.117647   0.117647   0.117647   0.117647      0.384314  0.392157  0.4
 0.117647   0.117647   0.117647   0.117647      0.384314  0.388235  0.392157
 0.121569   0.117647   0.121569   0.121569      0.388235  0.388235  0.388235
 0.12549    0.117647   0.12549    0.129412      0.396078  0.396078  0.396078

which represents a 224x224 image with 3 channels (RGB), but with 2 images. I want to split this apart so that I only have 1 image instead of two per array. How can I take this 4D array and split it?

I will note that the way I got the data in this form is via imgs = cat(imgs..., dims = 4) where imgs is a 3D array of images with two items, each representing an image.

Upvotes: 2

Views: 58

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69909

If array is your array then array[:, :, :, 1] and array[:, :, :, 2] should just work.

If you want an iterator use eachslice(array, dims=4) that allows you to iterate slices of the original array along the fourth dimension (this will create views into the original array).

Upvotes: 2

Related Questions