MATLAB Answers

How to solve preallocating speed of a variable?

33 views (last 30 days)
snr matlb
snr matlb on 21 Jan 2021
Commented: snr matlb on 21 Jan 2021
I have a variable which is object structure, I mean I define all properties in an m.file classfile and properties structure. Then I use them with a variable for example;
classdef obj<handle>
properties
id = zeros;
end
end
Then, I use for example "car" list in the for loop to fill the all properties, the number of properties is 30.
car = [];
for segment=1:5
for brand = 1:6
car(segment,brand).id = list(brand,'id');
..... till 30 properties like above
end
end
It works but the MATLAB gives suggestion to speed the script. The error*;
*The size of the indicated variable or array appears to be changing with each loop iteration. Commonly, this message appears because an array is growing by assignment or concatenation. Growing an array by assignment or concatenation can be expensive. For large arrays, MATLAB must allocate a new block of memory and copy the older array contents to the new array as it makes each assignment.
*Programs that change the size of a variable in this way can spend most of their run time in this inefficient activity. There is also significant overhead in shrinking an array on each iteration or in changing the size of a variable on each iteration. In such cases, MATLAB must reallocate and copy the contents repeatedly.
*For scripts, Code Analyzer cannot determine whether the array was preallocated before the script was called. Depending on your use of scripts, you might want to enable or disable this message independently of the related message (with message ID AGROW) that applies to functions and class methods.
How to make this done, variable has two indices like car(segment,brand).

  2 Comments

snr matlb
snr matlb on 21 Jan 2021
I solved it by starting the loop descending format to fix the last indices as the size of car object variable.
car = [];
for segment=5:-1:1
for brand = 6:-1:1
car(segment,brand).id = list(brand,'id');
..... till 30 properties like above
end
end
snr matlb
snr matlb on 21 Jan 2021
However, I concern about that I use car = [] and then I fill the car properties like car(segment, brand).id, car is normally empty array but I use it as an area for each car. I hope that that is a true approach.??

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 21 Jan 2021
Edited: Stephen Cobeldick on 21 Jan 2021
One simple solution is given here:
"To preallocate the object array, assign the last element of the array first."
You will have to make sure that your class returns a default value.

  1 Comment

snr matlb
snr matlb on 21 Jan 2021
Yes, I just made it, and is it the right way to work with object. After I define the objectstructure, I use;
car = []; % defining object name like this as an array
for segment=5:-1:1
for brand = 6:-1:1
car(segment,brand).id = list(brand,'id');
..... till 30 properties like above
end
end

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 21 Jan 2021
You start off with car an empty double array, then change it to a struct array with your first assignment in the loop. Instead I'd start off with car a struct array of the appropriate size. There are at least two ways to do this.
car1 = repmat(struct('id', NaN), [5 6])
car1 = 5x6 struct array with fields:
id
This creates a scalar struct with the appropriate field filled with missing data (NaN) and then replicates it.
car2 = struct('id', cell(5, 6))
car2 = 5x6 struct array with fields:
id
This takes advantage of an oft overlooked functionality in struct. If you call it and one of the fields is a cell array, struct will return a struct array the size of that cell array with each cell used to create the corresponding element of the struct array.
These two approaches do fill the id field with different data, but if you're just going to overwrite them that should be okay.
id1 = car1(1, 1).id
id1 = NaN
id2 = car2(1, 1).id
id2 = []

  1 Comment

snr matlb
snr matlb on 21 Jan 2021
That is super cool! But, I try to code particle swarm optimization so, I will use same cars with different particle, so, I have to walk through at each particle with same initial condition for cars. That is why I hold the same object name with different indices to produce the object with initial data automatically for each particle.
I am new at particle swarm, so I try to understand as well, maybe I will change my mind after I fail with my trial.
Thanks,

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!