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

조회 수: 3 (최근 30일)
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

답변 (2개)

yonatan gerufi
yonatan gerufi 2014년 10월 16일
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 2014년 10월 16일
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)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by