MATLAB class array


MATLAB class array



What would be the best way to manage large number of instances of the same class in MATLAB?

Using the naive way produces absymal results:

classdef Request     properties         num=7;     end     methods         function f=foo(this)             f = this.num + 4;         end     end end  >> a=[];    >> tic,for i=1:1000 a=[a Request];end;toc    Elapsed time is 5.426852 seconds.    >> tic,for i=1:1000 a=[a Request];end;toc   Elapsed time is 31.261500 seconds. 

Inheriting handle drastically improve the results:

classdef RequestH < handle     properties         num=7;     end     methods         function f=foo(this)             f = this.num + 4;         end     end end  >> tic,for i=1:1000 a=[a RequestH];end;toc Elapsed time is 0.097472 seconds. >> tic,for i=1:1000 a=[a RequestH];end;toc Elapsed time is 0.134007 seconds. >> tic,for i=1:1000 a=[a RequestH];end;toc Elapsed time is 0.174573 seconds. 

but still not an acceptable performance, especially considering the increasing reallocation overhead

Is there a way to preallocate class array? Any ideas on how to manage lange quantities of object effectively?

Thanks,
Dani




How to change a 2d array from dynamic to static of a specific size?

1:



Python Memory Model
This solution expands on Marc's answer.


How to assign array of pointers to content of static array?
Use repmat to initialize an array of RequestH objects and then use a loop to create the desired objects:.
Best way to remove repeats in a collection in Java?
>> a = repmat(RequestH,10000,1);tic,for i=1:10000 a(i)=RequestH;end;toc Elapsed time is 0.396645 seconds. 


Representing call center customer question logic
This is an improvement over:.
How to sort an array of integers correctly
>> a=[];tic,for i=1:10000 a=[a RequestH];end;toc Elapsed time is 2.313368 seconds. 


Limiting Array results


How can I compare different elements of array in Perl?

2:


Coming to this late, but would this not be another solution?.
a = Request.empty(1000,0); tic; for i=1:1000, a(i)=Request; end; toc; Elapsed time is 0.087539 seconds. 

Or even better:.
a(1000, 1) = Request; Elapsed time is 0.019755 seconds. 



3:


repmat is your friend:.
b = repmat(Request, 1000, 1);  Elapsed time is 0.056720 seconds   b = repmat(RequestH, 1000, 1); Elapsed time is 0.021749 seconds. 

Growing by appending is abysmally slow, which is why mlint calls it out..



76 out of 100 based on 46 user ratings 1246 reviews

*