# Why does \[1:3\] .^ 3 not work?

**URL:** https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767
**Category:** New to Julia
**Tags:** question
**Created:** [November 2, 2021, 12:10am UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767 "2021-11-02T00:10:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sclel016](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@sclel016](https://discourse.julialang.org/u/sclel016)
#### Post date: [November 2, 2021, 12:10am UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767/1 "2021-11-02T00:10:14Z")

</div>

Hi,

I’m a long time Matlab user trying to make the transition to Julia. It wasn’t obvious from reading the documentation whether the following behavior was a bug or just a characteristic of the language.

The following code throws an error, reporting no matching method in Julia 1.6.3

```julia
A = 1:10
B = A.^3

```

Is there a Julia-esque way to perform this operation or is this a bug?

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [November 2, 2021, 12:17am UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767/2 "2021-11-02T00:17:51Z")

</div>

Works for me in 1.6.1. Can you show your full session?

```julia

julia> A = 1:10
1:10

julia> B = A.^3
10-element Vector{Int64}:
    1
    8
   27
   64
  125
  216
  343
  512
  729
 1000

```

---

<div class="post-metadata">

### Author: ![sclel016](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@sclel016](https://discourse.julialang.org/u/sclel016)
#### Post date: [November 2, 2021, 12:24am UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767/3 "2021-11-02T00:24:42Z")

</div>

Looks like my Matlab habits bit me. I’m used to `[1:10]` being equivalent to `1:10`.

Definitely not the case in Julia, the brackets create a `Vector{UnitRange{Int64}}`. Thanks for the sanity check!

---

<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: [November 2, 2021, 1:05am UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767/4 "2021-11-02T01:05:40Z")

</div>

> [@sclel016](#):
>
> I’m used to `[1:10]` being equivalent to `1:10`

in MATLAB, a scalar is a 1x1 matrix so there’s no scalar.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 2, 2021, 1:22am UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767/5 "2021-11-02T01:22:35Z")

</div>

In Julia you could splat the range element of the vector to make it work:

```julia
julia> [1:3...].^3
3-element Vector{Int64}:
  1
  8
 27

```

---

<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: [November 2, 2021, 3:23am UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767/6 "2021-11-02T03:23:12Z")

</div>

While that works, it will be very inefficient for large ranges. It’s usually more idiomatic to write this as `[1:3;].^3` instead.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 2, 2021, 5:23am UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767/7 "2021-11-02T05:23:15Z")

</div>

> [@simeonschaub](#):
>
> It’s usually more idiomatic to write this as `[1:3;].^3`

But why? Why not just `(1:3).^3`?

---

<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: [November 2, 2021, 1:50pm UTC](https://discourse.julialang.org/t/why-does-1-3-3-not-work/70767/9 "2021-11-02T13:50:25Z")

</div>

Yeah, that might not have been a particularly good example. I just meant that if you really want to collect a range into a mutable vector for whatever reason that would be the preferable way to do it.
