# Replacing all array elements with a single value

**URL:** <https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847>\
**Category:** New to Julia\
**Created:** [October 13, 2019, 1:41am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847 "2019-10-13T01:41:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffC](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@JeffC](https://discourse.julialang.org/u/JeffC)\
**Post date:** [October 13, 2019, 1:41am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847/1 "2019-10-13T01:41:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 13, 2019, 1:43am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847/2 "2019-10-13T01:43:31Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![JeffC](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@JeffC](https://discourse.julialang.org/u/JeffC)\
**Post date:** [October 13, 2019, 1:52am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847/3 "2019-10-13T01:52:30Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![JeffC](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@JeffC](https://discourse.julialang.org/u/JeffC)\
**Post date:** [October 13, 2019, 2:02am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847/4 "2019-10-13T02:02:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 13, 2019, 2:07am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847/5 "2019-10-13T02:07:25Z")

</div>

> [@JeffC](#):
>
> How do I quote code?

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

```julia
your code surrounded by three backticks

```

---

<div class="post-metadata">

**Author:** ![JeffC](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@JeffC](https://discourse.julialang.org/u/JeffC)\
**Post date:** [October 13, 2019, 2:09am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847/6 "2019-10-13T02:09:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 13, 2019, 2:11am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847/7 "2019-10-13T02:11:13Z")

</div>

> [@JeffC](#):
>
> um(:,🙂 = -10;

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 13, 2019, 2:42am UTC](https://discourse.julialang.org/t/replacing-all-array-elements-with-a-single-value/29847/8 "2019-10-13T02:42:01Z")

</div>

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