0

I'm trying to optimize a given object oriented code in matlab. It is an economical model and consists of a Market and Agents. The time consuming part is to update certain attributes of all Agents during each timestep which is implemented in a for loop. However, I fail to vectorize the object oriented code.

Here is an example (Note, the second thing that slows down the code so far is the fact, that new entries are attached to the end of the vector. I'm aware of that and will fix that also):

for i=1:length(obj.traders)              
  obj.traders(i).update(obj.Price,obj.Sentiment(end),obj.h); 
end

Where update looks like

   function obj=update(obj,price,s,h)                          
           obj.pos(end+1)=obj.p;   
           obj.wealth(end+1)=obj.w(1,1,1);        
           obj.g(end+1)=s;                               
           obj.price=price;                                                 
           obj.Update_pos(sentiment,h);
           if (obj.c)                           
              obj.Switch_Pos;
           end
           ...

My first idea was to try something like

 obj.traders(:).update(obj.Price,obj.Sentiment(end),obj.h); 

Which didn't work. If someone has any suggestions how to vectorize this code, while keeping the object oriented implementation, I would be very happy.

1 Answer 1

1

I cannot provide a complete solution as this depends on the details of your implementation, but here are some tips which you could use to improve your code:

Remembering that a MATLAB object generally behaves like a struct, assignment of a constant value to a field can be done using [obj.field] =deal(val); e.g.:

[obj.trader.price] = deal(obj.Price);

This can also be extended to non-constant RHS, using cell, like so:

[aStruct.(fieldNamesCell{idx})] = deal(valueCell{:}); %// or deal(numericVector(:));

To improve the update function, I would suggest making several lines where you create the RHS vectors\cells followed by "simultaneous" assignment to all relevant fields of the objects in the array.


Other than that consider:

  1. setfield: s = setfield(s,{sIndx1,...,sIndxM},'field',{fIndx1,...,fIndxN},value);
  2. structfun: s = structfun(@(x)x(1:3), s, 'UniformOutput', false, 'ErrorHandler', @errfn);
  3. "A loop-based solution can be flexible and easily readable".

P.S. On a side note, I'd suggest you name the obj in your functions according to the class name, which would make it more readable to others, i.e.:

function obj=update(obj,price,s,h) => function traderObj=update(traderObj,price,s,h)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I will give this a try and let you know. Also thanks for the remark. This is not my code and I've already had a hard time trying to figure out which object the name "obj" refers to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.