From: Hugo on
Hi. To run my program, I need to create objects iteratively... This is what I have done:

for i=1:100
r(i)=RSpectrum(...)
end

where "RSpectrum" is the class used to build each r object and "..." are the required inputs.

However, I do not know how I can allocate memory for r. Can anyone help me out?

Many thanks in advance.
From: Sunny on
"Hugo " <hresquiveloa(a)gmail.com> wrote in message <hv86g0$b8b$1(a)fred.mathworks.com>...
> Hi. To run my program, I need to create objects iteratively... This is what I have done:
>
> for i=1:100
> r(i)=RSpectrum(...)
> end
>
> where "RSpectrum" is the class used to build each r object and "..." are the required inputs.
>
> However, I do not know how I can allocate memory for r. Can anyone help me out?
>
> Many thanks in advance.

Hi Hugo,

To do this, you need to allocate rows and columns for the element "r".

try:

for i = 1:100
r(i,:) = Rspectrum(...)
end

Or a variation of such
From: Hugo on
Sunny,

Thanks for reply...

My objects are:
r(1), r(2), r(3), ..., r(100)

But I need to find a way to preallocate r before treating to create them inside the loop. Do you know how can I do it?
From: Sunny on
"Hugo " <hresquiveloa(a)gmail.com> wrote in message <hv882o$nq8$1(a)fred.mathworks.com>...
> Sunny,
>
> Thanks for reply...
>
> My objects are:
> r(1), r(2), r(3), ..., r(100)
>
> But I need to find a way to preallocate r before treating to create them inside the loop. Do you know how can I do it?

Yes,
You can pre-allocate memory by creating a vector or matrix of zeros before your loop. This will speed up the time of the loop

For example:

r =zeros(1,100) ; %gives a 1x100 vector of 0

for i=1:100
%loop here
end

Cheers
From: Walter Roberson on
Sunny wrote:
> "Hugo " <hresquiveloa(a)gmail.com> wrote in message
> <hv882o$nq8$1(a)fred.mathworks.com>...
>> Sunny,
>>
>> Thanks for reply...
>>
>> My objects are:
>> r(1), r(2), r(3), ..., r(100)
>>
>> But I need to find a way to preallocate r before treating to create
>> them inside the loop. Do you know how can I do it?
>
> Yes,
> You can pre-allocate memory by creating a vector or matrix of zeros
> before your loop. This will speed up the time of the loop
>
> For example:
>
> r =zeros(1,100) ; %gives a 1x100 vector of 0
>
> for i=1:100
> %loop here
> end

Sunny, you are missing the point that each r is an _object_, not a double.

Hugo, the quick way to allocate would be to calculate r(100) first and
then to fill in r(1) through r(99) .

If I recall correctly there are examples of constructing arrays of
objects. I have never worked with that myself, so I do not recall the
technique.