Replacing all array elements with a single value

I am new to the Julia Language and like what I have seen so far. However, I have few questions. Here is one of them.

In Matlab, I can create an array of zeros
Example: ub=zeros(1,nz);
and replace all elements with a new value
Example: ub(:slight_smile: = -10
When I try
Example: ub[1,:] = -10; or ub[:] = -10;
I get the following error
ERROR: MethodError: no method matching setindex_shape_check(::Int64, ::Int64, ::Int64)

Can anyone help? Thanks!

In Julia, broadcast is not assumed nor inferred,
https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting-1

julia> a = rand(10)
10-element Array{Float64,1}:
 0.46400630682359223
 0.1957753520911425
 0.07286946443379883
 0.8514324789137262
 0.2822527883717596
 0.8221625781175499
 0.3247131615790402
 0.37229777035307743
 0.8315187849494032
 0.9406639580035234

julia> a .= 1; a
10-element Array{Float64,1}:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
2 Likes

Thank you!

One more question…what about a 2-D array? The same approach above didn’t work for it…

MATLAB: um(:,:slight_smile: = -10;

Thanks!
How do I quote code?

you quote code adding backticks `` ( one backtick for inline code) and three backticks for this:

your code surrounded by three backticks
2 Likes

Matlab example um(:,:) = -10;

add a dot in julia:
um[:,:] .= -10;
one advantage of this over matlab is that you can vectorize anything, for example:

f(x) = 2x + sin(x) #single variable to single variable
um[:,:] .=f.(10) #the dot means that the function is applied over all elements of the array
4 Likes

also maybe read this first
https://docs.julialang.org/en/v1/manual/noteworthy-differences/index.html

2 Likes