Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reshaping a broadcasted expression

Tags:

arrays

julia

I would like to make the following broadcasted expression work:

J = rand(4,4)
fx1 = rand(2,2)
fx2 = rand(2,2)
@. J[:,1] = fx1 + fx2

I really want some kind of:

@. J[:,1] = vec(fx1 + fx2)

where this vec is saying it should reshape to be 4x1, but I don't want to have to make this allocating. How could this be generically handled (i.e. no indexing on the fx)?

like image 690
Chris Rackauckas Avatar asked Nov 17 '17 07:11

Chris Rackauckas


1 Answers

Another possibility, is instead of vec-ing the fx1 and fx2 to reshape the slice of J:

Jcol = reshape(view(J,:,1),(2,2))
@. Jcol = fx1 + fx2

Not sure about efficiency, but it may give a clearer perspective depending on the surrounding algorithm. The LLVM code seems short enough and the assignment statement is clear.

like image 55
Dan Getz Avatar answered Oct 19 '22 06:10

Dan Getz