Reshaping array dimensions in Julia | Array reshape() Method

The reshape() is an inbuilt function in julia which is used to return an array with the same data as the specified array, but with different specified dimension sizes.

Syntax:
reshape(A, dims)

Parameters:

  • A: Specified array.
  • dims: Specified dimension.

Returns: It returns an array with the same data as the specified array, but with different specified dimension sizes.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of Array reshape() method
  
# Getting an array with the same data
# as the specified 1D array, but with
# (2 * 2) dimension sizes.
A = [1, 2, 3, 4];
println(reshape(A, (2, 2)))
  
# Getting an array with the same data
# as the specified 2D array, but with
# (4 * 4) dimension sizes.
B = [1  2  3  4; 5  6  7  8; 9  10  11  12; 13  14  15  16];
println(reshape(B, (4, 4)))
  
# Getting an array with the same data
# as the specified 2D array, but with
# (2 * 8) dimension sizes.
C = [1  5   9  13; 2  6  10  14; 3  7  11  15; 4  8  12  16];
println(reshape(C, 2, :))

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of Array reshape() method
  
# Getting an array with the same data
# as the specified 1D array, but with
# (2 * 2) dimension sizes.
A = ["a", "b", "c", "d"];
println(reshape(A, (2, 2)))
  
# Getting an array with the same data
# as the specified 2D array, but with
# (2 * 2) dimension sizes.
B = ["a" "b"; "c" "d"];
println(reshape(B, (2, 2)))
  
# Getting an array with the same data
# as the specified 3D array, but with
# (2 * 2*3) dimension sizes.
C = cat(["a" "b"; "c" "d"], ["e" "f"; "g" "h"], ["i" "j"; "k" "l"], dims = 3);
println(reshape(C, (2, 2, 3)))

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.