Julius K
Julius K

Reputation: 35

Initialize Array with shape

I want a matrix not the same shape as another matrix, but with the shape the other matrix holds.

shape=[2,3]
matrix=zeros(shape)
size(matrix)=(2,)

How can I get size(matrix)=shape?

I could do it like this:

matrix=zeros(shape[1],shape[2])

is there a more generic way to do it?

Upvotes: 1

Views: 185

Answers (1)

Dan Getz
Dan Getz

Reputation: 18217

try zeros(shape...). The splat operator ... turns a vector into parameters for a function:

v = [a,b,c]
func(v...)  # is the same as
func(a,b,c)

and it works for tuples too:

t = (a,b,c)
func(t...)  # is the same as
func(a,b,c)

Upvotes: 3

Related Questions