julialangHow to reshape arrays in JuliaLang?
JuliaLang provides a variety of functions to reshape arrays. The most commonly used functions are reshape() and permutedims().
reshape() is used to change the shape of an array without changing its data. For example,
julia> a = [1 2 3 4 5 6]
julia> reshape(a, 3, 2)
3×2 Array{Int64,2}:
1 4
2 5
3 6
permutedims() is used to rearrange the order of the dimensions of an array. For example,
julia> a = reshape(1:24, 4, 3, 2)
julia> permutedims(a, (3, 2, 1))
2×3×4 Array{Int64,3}:
[:, :, 1] =
1 5 9
2 6 10
[:, :, 2] =
3 7 11
4 8 12
[:, :, 3] =
13 17 21
14 18 22
[:, :, 4] =
15 19 23
16 20 24
The following are the parts of the code and their explanation:
a = [1 2 3 4 5 6]: This creates an arrayawith 6 elements.reshape(a, 3, 2): This reshapes the arrayainto a 3x2 array.a = reshape(1:24, 4, 3, 2): This creates an arrayawith 24 elements and reshapes it into a 4x3x2 array.permutedims(a, (3, 2, 1)): This rearranges the order of the dimensions of the arrayato 3x2x1.
Helpful links
More of Julialang
- How to test code in JuliaLang?
- How to use the println function in JuliaLang?
- How to use addprocs in JuliaLang?
- How to use regular expressions in JuliaLang?
- How to create plots in JuliaLang?
- How to convert a string to an integer in JuliaLang?
- How to use tuples in JuliaLang?
- How to install JuliaLang?
- How to calculate the mean in JuliaLang?
- How to add a legend to a plot in JuliaLang?
See more codes...