Broadcasting across arrays in Julia - GeeksforGeeks

24 Sep, 2021
Julia is a high performance, dynamic programming language that has a high-level syntax. It is free for everyone to use and easy to learn. Julia is used generally for data analysis and visualization purposes.
A grid that contains objects in multiple dimensions is called a Multi-dimensional Array. It can contain any type of object but for most computational purposes Int or Float type objects must be present. It is comparatively easier to implement Arrays in Julia than other computational languages. Arrays can be written in the code in an understandable manner and the compiler takes care of the implementation which is performance and time-efficient. In Julia, a custom array type can also be defined by inheriting from AbstractArray.
Sometimes it is very useful to apply operations on only some elements of an array, like adding a vector of two elements to the array. But it can be inefficient if it is done by increasing the size of the vector to the array’s size, especially for arrays with a large number of elements and more dimensions. 
Julia
# Creating two arrays with random elementsa = rand(2, 1);b = rand(2, 4); # Repeat function repeats the elements,# specified number of times.# Adding vector 'a' to 'b' by increasing the size of 'a'repeat(a, 1, 4) + b
Output: 
To tackle this problem, Julia provides the broadcast() function which increases the size of a singleton dimension array to the array we want to operate on and applies the function we provide, without using any extra memory. Hence, for large arrays, memory is not wasted. The function is valid not only for arrays but other structures too.
The arguments we can pass in the broadcast() function are as follows:
Operator/functionArray to be addedArray to be added on
Operator/function
Array to be added
Array to be added on
Applying the function on a 1D array: 
Julia
# Creating two 1D arrays of different sizesA = [1];B = [5,6,7,8]; # Adding 'A' to 'B' by increasing it's size# We use the addition operator in the broadcast functionbroadcast(+, A, B)
Applying the function on a 2D array:
Julia
# Applying the broadcast function# to perform addition on a 2D arrayA = [1 2];  B = [5 6; 7 8; 9 10; 11 12]; broadcast(+, A, B)
Applying the function on a 3D array:
Julia
# Applying the broadcast function# to perform addition on a 3D arrayA = [2 3]; B = cat([4 5;6 7], [8 9;10 11],                   [12 13;14 15], dims = 3);  broadcast(+, A, B)
We can use the dot operator (.+, .-, .*, .=) to implement the same operations we did with the broadcast() function
Using the dot operator on a 1D array:
Julia
# Equating all elements of the 1D array to the element '7'[1, 2, 3, 4].= [7]
  
Using the dot operator on a 2D array:
Julia
# Implementing addition, multiplication and# subtraction operations to broadcast to a 2D array[1,2].+[1 2 3; 4 5 6]  [1,2].*[1 2 3; 4 5 6]  [1,2].-[1 2 3; 4 5 6]
Using the dot operator on a 3D array:
Julia
# Implementing addition operator to broadcast to a 3D arraycat([4 5; 6 7] , [8 9; 10 11] , [12 13; 14 15] , dims = 3).+[1, 2]
Julia provides us with many functions that can broadcast on arrays, applying various operations. And we can implement these functions using the dot operator as shown below.
Julia
# Broadcasting to create strings with additional elements to a 1D arraystring.(1:3, ".", ["ONE", "TWO", "THREE"])
Julia
# Broadcasting to convert elements# of a 2D array to the type Float32convert.(Float32, [1 2 3; 4 5 6])
Julia
#  Broadcasting to calculate ceiling# of the decimal elements in a 3D arrayceil.((UInt8,), cat([4 5; 6 7] ,                    [8 9; 10 11] ,                    [12 13; 14 15] , dims = 3))
singghakshay
anikakapoor
julia-array
Picked
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Vectors in Julia
Getting rounded value of a number in Julia - round() Method
Decision Making in Julia (if, if-else, Nested-if, if-elseif-else ladder)
Storing Output on a File in Julia
Formatting of Strings in Julia
Manipulating matrices in Julia
Creating array with repeated elements in Julia - repeat() Method
Reshaping array dimensions in Julia | Array reshape() Method
while loop in Julia
Get array dimensions and size of a dimension in Julia - size() Method