Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


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


Re: Can my code be made more efficient using MATLAB's vectorization?
Posted:
Feb 16, 2013 6:55 PM


On 2/16/2013 4:51 PM, Jeff wrote: ...
> dpb <none@non.net> wrote in message <kfjhkn$8ea$1@speranza.aioe.org>... >> 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? > > dpb: > a) That is not what was intended. The fixed boundary code is not > complete (hopefully this is the "untested code" Bruno was referring too) > and should not have been included. Eventually, the fixed code will look > something like > > P_p = ([U(:,nPlusOne); 0]U(:,n)).^2; > > except that 0 will really have to be something like zeros(T,1). I'm > working on the periodic code first and will come back to that. > > dpb <none@non.net> wrote in message <kfjhkn$8ea$1@speranza.aioe.org>... >> 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 >> > > b) Are you talking about the code for P_p? I am subtracting the n > columns from the n+1 columns (that is, u(n+1) minus u(n)). I think you > read my code as u(n+1) minus u(n). I hope so, anyway. Because if you > read my code right and you think it is doing u(n+1) minus u(n1), then > in trouble! > > I think my code already does what circshift does, at least for the > periodic case. diff seems to be more for taking derivatives, so I don't > think it will help. ...
Yes, but you didn't define n so I presumed there was a typo and since you computed both a nPlusOne and a nMinusOne vector that the other vector was intended to be nMinusOne. If it's just 1:n then you don't need a subscript and :,n would reference one column (the nth one) unless n=[1:size(U,2)]
I haven't compared for relative speed but U(:,nPlusOne)U is identical to circshift(U,[0 1])U
Well, looking I see that circshift is an mfile so probably the specialcase precomputed indices will outperform the packaged more general function.
diff() will take care of the subtraction as well unless you're sure you do want the difference array to be the same sizeI was just checking that you think the wrapped position really does have meaning...if that's what you want, go for it.




