InPlace function writting into a subarray

94 views
Skip to first unread message

Ján Dolinský

unread,
Nov 21, 2014, 5:30:26 AM11/21/14
to julia...@googlegroups.com
Hello,

I wanted an inplace function to write a result into a subarray as follows:

X = zeros(10,5)
fill!(X[:,3], 0.1)

The X[:,3] is however not updated.

X[:,3] = fill(0.1, 10) does the update as expected.

Is this a desired behaviour ?


---

Alternatively, I can do

r = view(X,:,3)
fill!(r, 0.1)

This results in an updated column of X

I wonder which is likely to be more efficient if used in a loop:

X[:,3] = fill(0.1, 10)

or
r = view(X,:,3)
fill!(r, 0.1)


Thanks,
Jan

John Myles White

unread,
Nov 21, 2014, 5:31:44 AM11/21/14
to julia...@googlegroups.com
X[:, 3] doesn't produce a SubArray. It produces a brand new array.

-- John

Ján Dolinský

unread,
Nov 21, 2014, 8:06:32 AM11/21/14
to julia...@googlegroups.com
Thanks. Indeed, and that is why fill!(X[:,3], 0.1) does not update X. It updates the brand new array which is a copy of 3rd column of X. Intuitively, however, I would expect fill!(X[:,3], 0.1) to update the 3rd column of X.

Thanks for the clarification,
Jan

Milan Bouchet-Valat

unread,
Nov 21, 2014, 9:00:51 AM11/21/14
to julia...@googlegroups.com
Le vendredi 21 novembre 2014 à 05:06 -0800, Ján Dolinský a écrit :
> Thanks. Indeed, and that is why fill!(X[:,3], 0.1) does not update X.
> It updates the brand new array which is a copy of 3rd column of X.
> Intuitively, however, I would expect fill!(X[:,3], 0.1) to update the
> 3rd column of X.
Right, this will probably change soon in 0.4 when array views are made
the default.


Regards

Stefan Karpinski

unread,
Nov 21, 2014, 9:08:27 AM11/21/14
to julia...@googlegroups.com
You can just do X[:,3] = 0.1 and the value will be broadcast.

Ján Dolinský

unread,
Nov 21, 2014, 9:56:30 AM11/21/14
to julia...@googlegroups.com
Lovely :)
Reply all
Reply to author
Forward
0 new messages