The Wayback Machine - https://web.archive.org/web/20240927060354/https://www.geeksforgeeks.org/fill-an-array-with-specific-values-in-julia-array-fill-method/
Open In App

Fill an array with specific values in Julia | Array fill() method

Last Updated : 27 Jan, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The fill() is an inbuilt function in julia which is used to return an array of specified dimensions filled with a specific value passed to it as parameter.
 

Syntax: fill(Value, Dimension)
Parameters: 
 

  • Value: To be filled in the array
  • Dimension: Required size of the array

Returns: It returns an array of nXn dimension with each element as the specified value. 
 

Example: 
 

Python




# Julia program to illustrate
# the use of Array fill() method
 
# Creating a 1D array of size 4
# with each element filled with value 5
A = fill(5, 4)
println(A)
 
# Creating a 2D array of size 2X3
# with each element filled with value 5
B = fill(5, (2, 3))
println(B)
 
# Creating a 3D array of size 2X2X2
# with each element filled with value 5
C = fill(5, (2, 2, 2))
println(C)


Output: 
 

fill-julia-02

 

Array fill!() method

fill!() method works exactly like fill() method i.e. it fills an array with a specific value passed to it as argument, but the only difference is that, fill!() method takes an existing array as argument and fills it with a new specified value. While the fill() method takes array dimensions and creates a new array of its own. 
 

Syntax: fill!(Array, Value)
Parameters: 
 

  • Array: It is the array of specified dimension.
  • Value: It is the value to be filled in the array.

Returns: It returns the array passed to it as argument with the specified value filled at each index. 
 

Example: Below code is using one dimension array of 3 elements. 
 

Python




# Julia program to illustrate
# the use of Array fill() method
 
# Creating a 1D array of size 5
Array1 = [1, 2, 3, 4, 5]
 
# Filling array with fill!()
Array1 = fill!(Array1, 10)
println(Array1)
 
# Creating a 2D array of size 2X2
Array2 = [1 2; 3 4]
Array2 = fill!(Array2, 10)
println(Array2)
 
# Creating a 3D array of size 2X2X2
Array3 = cat([1 2; 3 4], [5, 6; 7 8], dims=3)
Array3 = fill!(Array3, 10)
println(Array3)


Output: 
 

fill-julia-01

 



Similar Reads

Julia end Keyword | Marking end of blocks in Julia
Keywords in Julia are reserved words that have a pre-defined meaning to the compiler. These keywords can't be used as a variable name. 'end' keyword in Julia is used to mark the end of a block of statements. This block can be of any type like struct, loop, conditional statement, module, etc. Syntax: block_type block_name Statement Statement end Exa
1 min read
Julia function keyword | Create user-defined functions in Julia
Keywords are the reserved words in Julia which have a predefined meaning to the compiler. These keywords are used to reduce the number of lines in code. Keywords in Julia can't be used as variable names. 'function' keyword is used to create user-defined functions in Julia. These functions are reusable codes that can be called from anywhere in the c
1 min read
Julia continue Keyword | Continue iterating to next value of a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can’t be used to name variables. 'continue' keyword in Julia skips the statement immediately after the continue statement. Whenever the continue keyword is executed, the compiler immediately stops iterating over further values and sends the execution
1 min read
Julia break Keyword | Exiting from a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can't be used to name variables. 'break' keyword in Julia is used to exit from a loop immediately. Whenever the break keyword is executed, the compiler immediately stops iterating over further values and sends the execution pointer out of the loop. S
1 min read
Julia local Keyword | Creating a local variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution.'local' keyword in Julia is used to create a variable of a limited scope whose value is local to the scope of the block in which it is defined. Synt
2 min read
Julia global Keyword | Creating a global variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution. 'global' keyword in Julia is used to access a variable that is defined in the global scope. It makes the variable where it is used as its current s
2 min read
Check if a specific index of an array contains a value in Julia - isassigned() Method
The isassigned() is an inbuilt function in julia which is used to test whether the given index contains a value in the specified array or not. Syntax: isassigned(array, i) Parameters: array: Specified array i: Specified index value Returns: It returns true if the specified index value is present in the array else false. Example 1: # Julia program t
2 min read
Accessing element at a specific index in Julia - getindex() Method
The getindex() is an inbuilt function in julia which is used to construct array of the specified type. This function is also used to get the element of the array at a specific index. Syntax: getindex(type, elements...])Parameters: type: Specified type.elements: Specified list of elements. Returns: It returns the constructed array of the specified t
2 min read
Get a substring of specific size from starting of a string in Julia - first() Method
The first() is an inbuilt function in julia which is used to return a string consisting of the first n characters of the specified string, where n will be passed as parameter. Syntax: first(s::AbstractString, n::Integer) Parameters: s::AbstractString: Specified string. n::Integer: Specified integer value. Returns: It returns a string consisting of
1 min read
Get a substring of specific size from end of a string in Julia - last() Method
The last() is an inbuilt function in julia which is used to return a string consisting of the last n characters of the specified string, where n will be passed as parameter. Syntax: last(s::AbstractString, n::Integer) Parameters: s::AbstractString: Specified string. n::Integer: Specified integer value. Returns: It returns a string consisting of the
1 min read
Get all array elements with true values in Julia | Array findall() Method
The findall() is an inbuilt function in julia which is used to return a vector of indices or keys of the all true values from the specified array A. If such true values are not present in the array, return an empty array. Here values of index or key start from 1 i.e, for index of 1st element is 1, index of 2nd element is 2 and so on. Syntax: findal
2 min read
Checking for true values in an array in Julia - any() and all() Methods
The any() is an inbuilt function in julia which is used to test whether any elements of the specified boolean collection are true and if any of the input value is 'missing', it will return missing if all non-missing values are false. Syntax: any(itr) Parameters: itr: Specified boolean collection. Returns: It returns true if any of the value of spec
2 min read
Getting type of keys and values of a Dictionary in Julia - keytype() and valtype() Methods
The keytype() is an inbuilt function in julia which is used to return the key type of the specified array. Syntax: keytype(A::AbstractArray) or keytype(T::Type{<:AbstractArray})Parameters: A::AbstractArray: Specified array.T::Type{<:AbstractArray}: Specified dictionary type. Returns: It returns the key type of the specified array. Example: Py
1 min read
Get previous true value from a given array index in Julia | Array findprev() Method
The findprev() is an inbuilt function in julia which is used to return the previous index before or including i of a true element of the specified array A, or returns zero if true value is not found. Here values of index or key start from 1 i.e, for index of 1st element is 1, index of 2nd element is 2 and so on. Syntax: findprev(A, i) or findprev(p
2 min read
Get next true value from a given array index in Julia | Array findnext() Method
The findnext() is an inbuilt function in julia which is used to return the next coming index after or including i of a true element of the specified array A, or returns zero if true value is not found. Here values of index or key start from 1 i.e, for index of 1st element is 1, index of 2nd element is 2 and so on. Syntax: findnext(A, i) or findnext
2 min read
Get index of first true value of array in Julia | Array findfirst() Method
The findfirst() is an inbuilt function in julia which is used to return the index or key of the first true value in the specified array. Here values of index or key start from 1 i.e, for index of 1st element is 1, index of 2nd element is 2 and so on. Syntax: findfirst(A) or findfirst(predicate::Function, A) Parameters: A: Specified array Predicate
2 min read
Get index of last true value of array in Julia | Array findlast() Method
The findlast() is an inbuilt function in julia which is used to return the index or key of the last true value in the specified array. Here values of index or key start from 1 i.e, for index of 1st element is 1, index of 2nd element is 2 and so on. Syntax: findlast(A) or findlast(predicate::Function, A) Parameters: A: Specified array Predicate Func
2 min read
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 specifi
2 min read
Getting parent array of a specified array in Julia - parent() Method
The parent() is an inbuilt function in julia which is used to return the parent array of a specified array view type (i.e, SubArray) or the array itself if it is not a view. Syntax: parent(A) Parameters: A: Specified array or an array view type. Returns: It returns the parent array of a specified array view type (i.e, SubArray) or the array itself
2 min read
Counting number of elements in an array in Julia - count() Method
The count() is an inbuilt function in julia which is used to count the number of elements in the specified array for which the given predicate p returns true and if p is omitted, counts the number of true elements in the given collection of boolean values. Syntax: count(p, itr) or count(itr) Parameters: p: Specified set of instructions. itr: Specif
2 min read
Get dimensions of array in Julia - ndims() Method
The ndims() is an inbuilt function in julia which is used to return the number of dimension of the specified array A. Syntax: ndims(A::AbstractArray) Parameters: A: Specified array Returns: It returns the number of dimension of the specified array A. Example 1: # Julia program to illustrate # the use of Array ndims() method # Finding the number of
1 min read
Get number of elements of array in Julia - length() Method
The length() is an inbuilt function in julia which is used to return the number of elements present in the specified array. Syntax: length(A::AbstractArray) Parameters: A: Specified array Returns: It returns the number of elements present in the specified array. Example 1: # Julia program to illustrate # the use of Array length() method # Finding t
2 min read
Get array dimensions and size of a dimension in Julia - size() Method
The size() is an inbuilt function in julia which is used to return a tuple containing the dimensions of the specified array. This returned tuple format is (a, b, c) where a is the rows, b is the columns and c is the height of the array. Syntax: size(A::AbstractArray) or size(A::AbstractArray, Dim) Parameters: A: Specified array Dim: Specified dimen
2 min read
Join an array of strings into a single string in Julia - join() Method
The join() is an inbuilt function in julia that is used to join an array of strings into a single string. The specified delimiter is used to insert in between the returned strings. Syntax: join([io::IO, ] strings, delim, last) Parameters: [io::IO, ] strings: Specified string.delim: Specified delimiter.last: If last is given then this last parameter
1 min read
Splitting string into array of substrings in Julia - split() and rsplit() Method
The split() is an inbuilt function in julia which is used to split a specified string into an array of substrings on occurrences of the specified delimiter(s). Syntax: split(str::AbstractString, dlm; limit::Integer, keepempty::Bool) or split(str::AbstractString; limit::Integer, keepempty::Bool) Parameters: str::AbstractString: Specified string. Dlm
2 min read
Iterating over each index of array in Julia - eachindex() Method
The eachindex() is an inbuilt function in julia which is used to create an iterable object for visiting each index of the specified array. Syntax: eachindex(A...) Parameters: A: Specified array. Returns: It returns an iterable object for visiting each index of the specified array. Example 1: # Julia program to illustrate # the use of Array eachinde
2 min read
Reshaping array as a vector in Julia - vec() Method
The vec() is an inbuilt function in julia which is used to reshape the specified array as a one-dimensional column vector i.e, 1D array. Syntax: vec(a::AbstractArray) Parameters: a::AbstractArray: Specified array. Returns: It returns the reshaped 1D array. Example 1: # Julia program to illustrate # the use of Array vec() method # Reshaping the spec
2 min read
Creating array with repeated elements in Julia - repeat() Method
The repeat() is an inbuilt function in julia which is used to construct an array by repeating the specified array elements with the specified number of times. Syntax: repeat(A::AbstractArray, counts::Integer...) or repeat(A::AbstractArray; inner, outer) or repeat(s::AbstractString, r::Integer) or repeat(c::AbstractChar, r::Integer) Parameters: A::A
2 min read
Getting an array of all items of a collection in Julia - collect() Method
The collect() is an inbuilt function in julia which is used to return an array of all items in the specified collection or iterator. Syntax: collect(collection) or collect(element_type, collection) Parameters: collection: Specified collection or iterator. element_type: Specified type of elements. Returns: It returns an array of all items in the spe
1 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: # Julia program to illustrate # the use of first() method # Getting th
1 min read
Article Tags :