Open In App

Set elements at a given index of array in Julia – setindex!() Method

The setindex!() is an inbuilt function in julia which is used to store values from the given array X within some subset of A as specified by inds.
 

Syntax: 



setindex!(A, X, inds...)

Parameters: 

Returns: It does not return any values. 
 
Example 1: 






# Julia program to illustrate
# the use of Array setindex!() method
 
# This function store values from an array
# X within some subset of A as
# specified by inds.
A = zeros(2, 2);
setindex !(A, [5, 10], [1, 2]);
A[[3, 4]] = [15, 20];
println(A)

Output: 
 

Example 2: 




# Julia program to illustrate
# the use of Array setindex!() method
 
# This function store values from an array
# X within some subset of A as
# specified by inds.
A = ones(2, 2);
setindex !(A, [2, 4], [1, 2]);
A[[3, 4]] = [6, 8];
println(A)

Output: 
 

 

Article Tags :