Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Modifying the same field in an array of Mutable Structs

Tags:

struct

julia

I am looking for the best way to adjust the same field, y, of an array of mutable structs, x to the same number.

x[:].y=0 kind of thing. In matlab I've seen it done using the deal() function. How can I do this in Julia?

like image 246
Phillip Sutton Avatar asked Feb 12 '26 10:02

Phillip Sutton


2 Answers

You can use broadcasting:

setproperty!.(x, :y, 0)
like image 170
Bogumił Kamiński Avatar answered Feb 14 '26 22:02

Bogumił Kamiński


Don't be afraid of writing the most obvious thing: a loop! It will be efficient in Julia. For example something like

for el in x
    el.y = 0
end
like image 39
fredrikekre Avatar answered Feb 14 '26 23:02

fredrikekre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!