I want to initalize structures and it seems to be too slow. How do I do it with repmat, which is supposed to be a much faster solution in Matlab? Originally:
for i=1:30
myloc.one.matrixBig(i,1).matrixBig= zeros(6,6);
for j=1:5
myloc.one.id(i,j) = 0;
for k=1:10
myloc.one.final(j,k).final(i,1) = 0;
end
end
end
EDIT:
for j=1:30
for i=1:10
myObject{i,j}.s = zeros(6,1);
myObject{i,j}.f = zeros(6,1);
end
end
Also, am I able to make it faster by adding some [] initialization even before, or is that limit of my optimization possibilities? Thanks very much for help!
Well in this case the first question is why are you using a cell array of structs. One of the fundamentals of matlab is that everything can be vectorised. So instead set up a matrix of structs.
A matrix of structures is a far more powerful datatype. For example if you want to extract a single row of the field in to a vector you can simply use the following syntax
Here's the equivalent vectorized code of the first code snippet:
or alternatively:
Choose the one you like most.
As for the second (edited) part, it can be replaced with:
Note that there is no need to preallocate anything because there aren't any explicit loops here.