user0347284
user0347284

Reputation: 31

Vector as an exponent in Julia

I have very basic question. I am new to Julia and used to code in R a lot. I need to take a scalar to the multiple powers, represented by a vector: 3^[2,3]. I got an error "Method error: no method matching ^...". I tried 3^Array([2,3]), but got the error again. At the same time, 3*[2,3] works as expected. Is there any way to do it in Julia without using for loop?

Upvotes: 2

Views: 2581

Answers (2)

Alexander Morley
Alexander Morley

Reputation: 4181

I think you are looking for the . or broadcast functions that allow you to apply any other functions elementwise!

3 .^ [2,3] or broadcast(^, 3, [2,3])

Upvotes: 10

kipp
kipp

Reputation: 56

Small edit: you'll need a space after the number to be exponentiated, e.g. 3 .^[2,3].

Upvotes: 1

Related Questions