From: Richard on
Hi, is it possible to vectorise computations on arrays of user defined objects?
for example, I have an array of myObject that contains 400 myObject elements.
I can access individuals using myObject(i).

I'm using
for i=1:length(myObject)
myObject(i).property=myObject(i).property^2;
end


wouldn't it be much faster if I could do

myObject(:).property=myObject(:).property^2;

Can this be done? Is my problem with syntax ?
From: Matt J on
"Richard " <REMOVETHISrcaldwellie(a)yahoo.com> wrote in message <i4119r$m2q$1(a)fred.mathworks.com>...

> wouldn't it be much faster if I could do
>
> myObject(:).property=myObject(:).property^2;
=================

Probably not. You have such a short for-loop that it's hard to improve upon.
You can condense the operation to 1 statement using arrayfun

myObject=arrayfun(@(ob) set(ob,'property',ob.property.^2),myObject)

but it probably won't be faster.
From: Matt J on
"Richard " <REMOVETHISrcaldwellie(a)yahoo.com> wrote in message <i4119r$m2q$1(a)fred.mathworks.com>...

> wouldn't it be much faster if I could do
>
> myObject(:).property=myObject(:).property^2;
>
> Can this be done? Is my problem with syntax ?
================

The better thing to do would be to have a single myObject, instead of an array of them and store the property values as a vector in myObject.property. Then you could just do this

myObject.property=myObject.property.^2;