How to initialize object array with different parameters?

This is the sort of thing I want to do:
Define class:
classdef Class
properties
prop
end
methods
function obj = Class(param)
if nargin
obj.prop = param;
end
end
end
end
Then initialize object array like so:
dim = {3,2};
param = 1:6;
obj_arr(dim{:}) = Class(param)
and get and object array like so:
obj_arr =
2x3 Class array with properties:
prop
obj_arr(1,1).prop =
1
obj_arr(2,1).prop =
2
...
obj_arr(3,2).prop =
6
I have one idea that involves using a static method, but I'm having trouble implementing it, and it feels messy to me.
Also, I know that initalizing the object array like that doesn't work. I guess the idea of initializing an object array is to not pass any parameters and have all of them as clones of one another?
The whole reason for asking this question is because right now I'm doing the preallocation, then using a for loop to intiatiate each object (patient), which takes a lot of time, since I've implemented a particle swarm optimization for each object (patient). I want to be able to have the particle swarm optimization run for all the objects (patients) in the cohort at the same time.
Also, I've tried parfor, and it doesn't help any. I have 27 objects (patients) in my cohort.

 Accepted Answer

With the current interface, this is probably the best you can do:
dim = [3,2];
param = 1:6;
param = num2cell(param); %each object param must be an element of a cell array
obj_arr(prod(dim)) = Class; %initialise all empty
obj_arr = reshape(obj_arr, dim); %reshape into desired shape
[obj_arr.prop] = param{:}; %deal each parameter to each object
If you use the same interface as the struct function where if the input is a cell array, the output is a structure array, then you could have this:
classdef Class %what a dangerous name! Too close to the all useful: class
properties
prop
end
methods
function obj = Class(param)
if nargin
if iscell(param)
[obj(1:numel(param)).prop] = param{:};
else
obj.prop = param;
end
end
end
end
end
%usage:
param = num2cell(1:6);
obj_arr = Class(param) %create a 1x6 array
celldisp({obj.arr}) %verify that each object got a single param.

4 Comments

Thank you very much, Guillaume!
The second solution is what I needed. It's messier than I was hoping but does the trick. The main thing isn't really to set the parameters over the array of objects as much as it is applying the particleswarm for each object. In the end, I used arrayfun for that purpose in combination of what you showed here. It looks like runtime now is about 1/6 times, so I'm very excited about that. :D
I personally don't think the second solution is messy. It encapsulate the array creation in the class constructor rather than forcing the caller to do it. To me, it's neater.
With regards to your optimisation function, if that function is a method of the class, if you also make it array-aware then you wouldn't need arrayfun (or rather, that bit would happen inside the function).
Okay, really my implementation of solution is a little messy. Just wanted to get it working first. Now I can clean up what I currently have. And looking back at what you wrote, I think it is pretty neat/elegant. And good point about arrayfun. Thanks again!
lets say my class had 2 properties, how could I go about doing something similar.
I want an array of my object that are initialised with different paramaters for each property.
Sorry im very new to matlab, any help would be appreciated.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!