Preallocating zeros in cell array. and Which one is faster? 3D matrix, cell or structure data?

3 views (last 30 days)
Hello all, I have to rewrite my Matlab code to make it faster. in some cases I need to used cell structure to keep my data: for example:
wallP = cell(1,wallDensity+1);
for b = wallDensity+1:-1:1
for u = 1:nWalls
wallP{b}(u,:) = center + direction(u,:).*(wall+(((1-wall)/wallDensity)*(b-1)));
end
end
1. I did preallocate my cell matrix, but my question is that it's not like zeros matrix the result in just some empty matrix however I know that after this loop I have a matrix of (nWalls,3) in each cell can I make a zero matrix to enhance the speed? do you recommend it?
(preallocating)
wallP = cell(1,wallDensity+1)
wallP =
[] [] []
(the final result)
wallP =
[16x3 double] [16x3 double] [16x3 double]
is there any way to make a zeros of (16,3) in each cell before the loop?
2. My second question is that it's possible for me to make a 3D matrix or create a structure to keep these data, for example: wallP(u).b instead of wallP{b}(u,:) or any other way that I don't know??!!!! Thanks for your help

Answers (2)

yonatan gerufi
yonatan gerufi on 16 Oct 2014
Hi Masha, first of all, regarding to code execution time see " profiler ".
it's a matlab feature that analyze the running time of your code.
you can switch part of your code and track the time difference.
regarding the second question (please edit the first one).
read this: cell vs. matrix

Sean de Wolski
Sean de Wolski on 16 Oct 2014
If each matrix (cell element) is the same size, then you're best off making a 3d matrix
16x3xp
where p is the number cells.
You can then index into it, for example the second one:
x(:,:,2)

Community Treasure Hunt

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

Start Hunting!