From: dpb on 13 May 2010 14:22 db wrote: ....[top posting repaired...please in future don't do that; hard conversation follow it makes]... .... >> I'll ask OP straight out--why _not_ as you wrote it? .... > I happened to see a logic that use if end statement for stock > backtesting logic and I couldn't get it to work with if end statement .... > if(c(end) >= uppr(end)) > postion = 1; > end > if(c(end) <= lowr(end)) > position = -1; > end Well, yes... "end" is overloaded in Matlab; in this usage in the logical test it is the subscript of the last entry in the arrays so c(end), for example is a single value. doc end % for more detail --
From: Steven Lord on 13 May 2010 14:44 "dpb" <none(a)non.net> wrote in message news:hsgvc9$qml$1(a)news.eternal-september.org... > Lior Cohen wrote: >> dpb <none(a)non.net> wrote in message > ... > >> you can do it by for loop and if statement on every single element, but I >> really do not see the point of this. The vectorized logical index you use >> is better. >> Lior > > yes, sorry for empty post. > > Agree; seems logical way to approach the question... :) > > I'll ask OP straight out--why _not_ as you wrote it? Vectorization is a trade-off. Instead of having to perform N operations in a loop, you can perform 1 operation on a vector of N elements. If you need to create temporary variables that are the same size as the data upon which you're operating, you're trading off operations for memory. If the OP's data was so large and/or their operations involved enough temporary variables that constructing those temporaries (in contiguous memory) caused memory to fragment or caused the program to run out of memory, breaking the computations down (to either N operations on scalars, or perhaps a number of operations on smaller vectors) could allow the computation to succeed. Vectorization is a hammer. But not every problem involves driving nails. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: dpb on 13 May 2010 20:47 Steven Lord wrote: > "dpb" <none(a)non.net> wrote in message > news:hsgvc9$qml$1(a)news.eternal-september.org... >> Lior Cohen wrote: >>> dpb <none(a)non.net> wrote in message >> ... >> >>> you can do it by for loop and if statement on every single element, but I >>> really do not see the point of this. The vectorized logical index you use >>> is better. >>> Lior >> yes, sorry for empty post. >> >> Agree; seems logical way to approach the question... :) >> >> I'll ask OP straight out--why _not_ as you wrote it? > .... > Vectorization is a hammer. But not every problem involves driving nails. Certainly, but OP gave no hint of any problem other than syntax of constructing an if...end block instead. I was trying to elicit such if there was anything else. --
First
|
Prev
|
Pages: 1 2 Prev: code operating speed is very slow Next: Different answers on different CPUs? |