dpb
Posts:
9,332
Registered:
6/7/07


Re: Can my code be made more efficient using MATLAB's vectorization?
Posted:
Feb 14, 2013 3:32 PM


On 2/14/2013 11:49 AM, Jeff wrote: > Is there a better, more MATLABy way, to write this code (that is, > using vectors)? Eventually, this will be used for large amounts of > data (from a program running on a cluster), so I need it as efficient > as possible. > > U and W are T rows x N columns matrices. Variable evals is a 1 by N > matrix of the eigenvalues (of some other matrix which doesn't matter > for this post). For now, the largest value of T is about 1000, but > eventually it will be many thousands (maybe even millions). > > So the first thing I need to do (variable P_p) is subtract the each > entry on the row from the one to its right (for now, I only have > periodic conditions coded). I think I have that variable coded fairly > efficiently (yes? no?). > > if strcmp(boundary,'periodic') > nMinusOne=[N 1:N1]; > nPlusOne=[2:N 1]; > elseif strcmp(boundary,'fixed') > nMinusOne=[1:N1]; > nPlusOne=[2:N]; > end
...
First, a couple of questions...
a) The periodic solution as written will be N columns whereas the fixed will be N1. Is this intended/desired?
b) As written the difference between n+  n of the array for periodic will be that of column n minus n2 rather than the "one on its right" which implies a first difference. To illustrate if N were 5, say, the two column vectors you generate are
[5 1 2 3 4] [2 3 4 5 1]
which results in deltas of 25, 31, 42, etc., ... Is this intended or did you intend
[5 1 2 3 4] [1 2 3 4 5]
instead?
W/ those answers, then can progress to next step (altho I'll note you may find
doc diff doc circshift
of interest)...


