# Converting - for loop to Cuarray dot syntax

**URL:** https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679
**Category:** General Usage
**Tags:** cuarrays
**Created:** [February 20, 2021, 3:19pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679 "2021-02-20T15:19:52Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![naviinkapoor](https://avatars.discourse-cdn.com/v4/letter/n/977dab/32.png) [@naviinkapoor](https://discourse.julialang.org/u/naviinkapoor)
#### Post date: [February 20, 2021, 3:19pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/1 "2021-02-20T15:19:52Z")

</div>

Hello Everyone,

I need help to convert the below code to carry dot syntax

From

for i=1:j

a[i] = a[i] + a[i+j]

end

I converted it to a[1:j].=a[1:j].+a[1:j+j] , but it is not working

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 20, 2021, 3:29pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/2 "2021-02-20T15:29:24Z")

</div>

You are adding arrays of different lengths.

---

<div class="post-metadata">

### Author: ![naviinkapoor](https://avatars.discourse-cdn.com/v4/letter/n/977dab/32.png) [@naviinkapoor](https://discourse.julialang.org/u/naviinkapoor)
#### Post date: [February 20, 2021, 3:36pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/3 "2021-02-20T15:36:02Z")

</div>

I got it, but is there any way to implement this in Cuarray dot syntax?

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [February 20, 2021, 3:39pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/4 "2021-02-20T15:39:50Z")

</div>

> [@naviinkapoor](#):
>
> converted it to a[1:j].=a[1:j].+a[1:j+j] , but it is not working

You may be looking for `a[1:j].=a[1:j].+a[(1:j).+j]`. `+` takes precedence over `:`, so `1:j+j` is equivalent to `1:(j+j)` and therefore probably not what you meant.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [February 20, 2021, 3:42pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/5 "2021-02-20T15:42:24Z")

</div>

Your error is in `1:j+j`. This evaluates to `1:(j+j)`. You can solve it by writing it as `(1:j).+j`.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 20, 2021, 3:43pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/6 "2021-02-20T15:43:36Z")

</div>

What do you mean by CuArray dot syntax?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [February 20, 2021, 3:48pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/7 "2021-02-20T15:48:50Z")

</div>

It already works with `CuArray`s:

```julia
julia> n = 5

julia> a = CuArray(fill(1, 2n));

julia> a[1:n] .+= a[n+1:2n];

julia> a
10-element CuArray{Int64, 1}:
 2
 2
 2
 2
 2
 1
 1
 1
 1
 1

```

---

<div class="post-metadata">

### Author: ![naviinkapoor](https://avatars.discourse-cdn.com/v4/letter/n/977dab/32.png) [@naviinkapoor](https://discourse.julialang.org/u/naviinkapoor)
#### Post date: [February 20, 2021, 3:59pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/8 "2021-02-20T15:59:24Z")

</div>

> [@gustaphe](#):
>
> `(1:j).+j` .

Perfect, thanks it is working:)

---

<div class="post-metadata">

### Author: ![naviinkapoor](https://avatars.discourse-cdn.com/v4/letter/n/977dab/32.png) [@naviinkapoor](https://discourse.julialang.org/u/naviinkapoor)
#### Post date: [February 20, 2021, 3:59pm UTC](https://discourse.julialang.org/t/converting-for-loop-to-cuarray-dot-syntax/55679/9 "2021-02-20T15:59:37Z")

</div>

Thanks it is also working:)
