JeffC
October 13, 2019, 1:41am
1
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( = -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!
jling
October 13, 2019, 1:43am
2
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
JeffC
October 13, 2019, 2:02am
4
One more question…what about a 2-D array? The same approach above didn’t work for it…
MATLAB: um(:, = -10;
Thanks!
How do I quote code?
JeffC:
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
JeffC
October 13, 2019, 2:09am
6
Matlab example um(:,:) = -10;
JeffC:
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
jling
October 13, 2019, 2:42am
8
2 Likes