Broadcasting across arrays in Julia
Last Updated :
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.
Broadcasting across Arrays
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 elements
a = 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/function
- Array to be added
- Array to be added on
Applying the function on a 1D array:
Julia
# Creating two 1D arrays of different sizes
A = [1];
B = [5,6,7,8];
# Adding 'A' to 'B' by increasing it's size
# We use the addition operator in the broadcast function
broadcast(+, A, B)
Applying the function on a 2D array:
Julia
# Applying the broadcast function
# to perform addition on a 2D array
A = [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 array
A = [2 3];
B = cat([4 5;6 7], [8 9;10 11],
[12 13;14 15], dims = 3);
broadcast(+, A, B)

Using dot operator
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 array
cat([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 array
string.(1:3, ".", ["ONE", "TWO", "THREE"])
Julia
# Broadcasting to convert elements
# of a 2D array to the type Float32
convert.(Float32, [1 2 3; 4 5 6])
Julia
# Broadcasting to calculate ceiling
# of the decimal elements in a 3D array
ceil.((UInt8,), cat([4 5; 6 7] ,
[8 9; 10 11] ,
[12 13; 14 15] , dims = 3))
Similar Reads
Cell Arrays in Julia
Cell array is an abstract data type with indexed data containers called cells, where each cell can contain any type of data. It is normally used in Matlab to store data.Coming to Julia, one of the magical things about Julia is its type system. It is a strictly typed language. In Julia, arrays can co
5 min read
Arrays in Julia
Arrays in Julia are a collection of elements just like other collections like Sets, Dictionaries, etc. Arrays are different from Sets because arrays are ordered collection of elements, and can hold duplicate values, unlike sets which require all the elements to be unique. Arrays are N-Dimensional co
13 min read
Array Interfaces in Julia
An array interface is a syntactical contract for arrays in Julia that they must follow. This article describes the various methods which can be adopted to construct array interfaces in Julia. It also explains how to perform indexing in an array interface to access its elements. An array interface ca
5 min read
Characters in Julia
Julia is a dynamic, high-level programming language with high performance and speed that is used to perform operations in scientific computing. It is great for computational complex problems. It is an open-source language so all source code is available easily online. It is as easy to use as Python
2 min read
Exploratory Data Analysis in Julia
Exploratory Data Analysis (EDA) is used for achieving a better understanding of data in terms of its main features, the significance of various variables, and the inter-variable relationships. The First step is to explore the dataset. Â Methods to explore the given dataset in Julia: Using data table
4 min read
Importing data from Files in Julia
Julia supports File Handling in a much easier way as compared to other programming languages. Various file formats can easily be loaded in our Julia IDE. Most of the file extension packages are loaded into the package, named Pkg in Julia. This basically adds the package needed to load data of differ
5 min read
Benchmarking in Julia
In Julia, most of the codes are checked for it's speed and efficiency. One of the hallmarks of Julia is that it's quite faster than it's other scientific computing counterparts(Python, R, Matlab). To verify this, we often tend to compare the speed and performance of a code block run across various l
6 min read
try keyword - Handling Errors in Julia
Keywords in Julia are words that can not be used as a variable name because they have a pre-defined meaning to the compiler. 'try' keyword in Julia is used to intercept errors thrown by the compiler, so that the program execution can continue. This helps in providing the user a warning that this cod
2 min read
Array creation using Comprehensions and Generators in Julia
Julia is a language designed for high-level performance and can support interactive use as well. It has many descriptive datatypes and type-declarations can be used to solidify the programs. Julia is slowly climbing the ladder and gaining the interest of many Data Scientists and machine learning sci
2 min read
Getting first element of an array in Julia - first() Method
The first() is an inbuilt function in julia which is used to return the first element of the specified iterable collection. Syntax: first(coll) Parameters: coll: Specified iterable collection. Returns: It returns the first element of the specified iterable collection. Example 1: Python # Julia progr
1 min read