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 arraya
with 6 elements.reshape(a, 3, 2)
: This reshapes the arraya
into a 3x2 array.a = reshape(1:24, 4, 3, 2)
: This creates an arraya
with 24 elements and reshapes it into a 4x3x2 array.permutedims(a, (3, 2, 1))
: This rearranges the order of the dimensions of the arraya
to 3x2x1.
Helpful links
More of Julialang
- How to test code in JuliaLang?
- How to round numbers in JuliaLang?
- How to get JuliaLang version?
- How to use tuples in JuliaLang?
- How to use the JuliaLang package manager?
- How to install JuliaLang?
- How to solve differential equations in JuliaLang?
- How to use try catch in JuliaLang?
- How to use regular expressions in JuliaLang?
- How to calculate the mean in JuliaLang?
See more codes...