memory - Are single loops or dense loops more computationlly efficent in matlab? -
im writing code implement numerical approximation 3d steady state heat equation using finite difference matrix methods. involves discritising 2nd order pde matrix , solving ax=b. x temperature @ each of specified grid points. further information on type of question can found here:
http://people.nas.nasa.gov/~pulliam/classes/new_notes/matrix_ode.pdf
to complete problem, have represented 3d matrix 2d array calling values in 1d array b using indexing function of form:
i+(j-1)*nx+nx*ny*(k-1) for (i,j,k)th element of 3d matrix nx, ny, nz number of points in x,y,z coordinates. there ends being lot of loop computation in order create matrix , b , wondering computationally efficient , less memory exhaustive way run these loops, i.e. better use like
for j=1:ny i=2:nx-1 b(i+(j-1)*nx)=d4; end end j=1:ny i=2:nx-1 b(i+(j-1)*nx+nx*ny*(nz-1))=d3; end end or should condense these single loop like:
for j=1:ny i=2:nx-1 b(i+(j-1)*nx)=d4; b(i+(j-1)*nx+nx*ny*(nz-1))=d3; end end i have preallocated both arrays , b. there vectorised way also?
the second method should faster since performs same number of calculations fewer increments of loop variables. can matlab's built-in stopwatch commands tic , toc time code. http://www.mathworks.com/help/matlab/ref/tic.html
something more vectorized might possible need know more format of arrays contain d3 , d4. reshape() function might able help.
Comments
Post a Comment