'Create matrix using comprehension in julia

I want to create a diagonal matrix whose diagonals are powers of 1/2. I know I could do:

A = Diagonal(1, 1/2, 1/2^2, ..., 1/2^10)

I was wondering if there is a way to do this using a comprehension, something like this:

A = Diagonal((1/2)^x for x=[0:10]) 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source