|
From: Jack on 19 Jun 2008 09:50 MathWorks have to build this feature in MATLAB!! !!??How long in 21 century all of us will continue to write such a stupid obsolete loops: tmp1=0; for n=1:N tmp2=0 for m=1:M(n) tmp1=tmp1+1/b(m,n); end; tmp2=tmp2+a(n)*tmp1; end; INSTEAD OF THIS SYNTAXIS: sum(n=1:N, a(n)*sum(m=1:M(n),1/b(m,n)) ) 99% of all mathematics and algorithms will be simpe...
From: Jos on 19 Jun 2008 10:36 "Jack " <jackfrost(a)list.ru> wrote in message <g3do6r$71m$1(a)fred.mathworks.com>... > MathWorks have to build this feature in MATLAB!! > > !!??How long in 21 century all of us will continue to write > such a stupid obsolete loops: > > tmp1=0; > for n=1:N > tmp2=0 > for m=1:M(n) > tmp1=tmp1+1/b(m,n); > end; > tmp2=tmp2+a(n)*tmp1; > end; > > INSTEAD OF THIS SYNTAXIS: > > sum(n=1:N, a(n)*sum(m=1:M(n),1/b(m,n)) ) > > 99% of all mathematics and algorithms will be simpe... > > > > > I am almost sure these loops can be vectorized, provided you give some details on your variables N, M, a & b. Jos
From: John D'Errico on 19 Jun 2008 10:58 "Jos " <DELjos(a)jasenDEL.nl> wrote in message <g3dqsh$bjq$1(a)fred.mathworks.com>... > "Jack " <jackfrost(a)list.ru> wrote in message > <g3do6r$71m$1(a)fred.mathworks.com>... > > MathWorks have to build this feature in MATLAB!! > > > > !!??How long in 21 century all of us will continue to write > > such a stupid obsolete loops: > > > > tmp1=0; > > for n=1:N > > tmp2=0 > > for m=1:M(n) > > tmp1=tmp1+1/b(m,n); > > end; > > tmp2=tmp2+a(n)*tmp1; > > end; > > > > INSTEAD OF THIS SYNTAXIS: > > > > sum(n=1:N, a(n)*sum(m=1:M(n),1/b(m,n)) ) > > > > 99% of all mathematics and algorithms will be simpe... > > > > > > > > > > > > > I am almost sure these loops can be vectorized, provided you > give some details on your variables N, M, a & b. > > Jos If b is truly a square matrix, then the vectorization is trivial using bsxfun, etc. Assuming the OP wants to sum over only parts of b, with the length of the sum varying per column, then there are sill easy ways to solve it. I might choose to mask off the unused elements, or use an indexing scheme. If this was something the OP does often, then a function of their own is best. This is what makes Matlab great - the ability to customize it to your own special needs. John
From: Jack on 20 Jun 2008 03:14 > If b is truly a square matrix, then the vectorization > is trivial using bsxfun, etc. > > John no, it is impossible to vectorize everything. also it is imposible to customize evry need. sum(n=1:N, equation*sum(m=1:M(n) ,...equation...) ) Sometimes (realy very often) the equation behind the sum cannot be evaluted via all indexes before the function call. Also it cannot be writen in C/C++ or Java. Only Python support such constructions.
From: Jos on 20 Jun 2008 04:13
"Jack " <jackfrost(a)list.ru> wrote in message <g3flbq$lmi$1(a)fred.mathworks.com>... > > > If b is truly a square matrix, then the vectorization > > is trivial using bsxfun, etc. > > > > John > > no, it is impossible to vectorize everything. also it is > imposible to customize evry need. > > sum(n=1:N, equation*sum(m=1:M(n) ,...equation...) ) > > Sometimes (realy very often) the equation behind the sum > cannot be evaluted via all indexes before the function > call. > Also it cannot be writen in C/C++ or Java. Only Python > support such constructions. > > Why are you so stubborn? As John has pointed out, Matlab's flexibility allows you to write your own function, that does the job. I also suspect your for-loop is flawed, since you keep resetting your tmp2 variable to zero. Your loop bowls down to a much simpler one, which can be vectorized easily ... % example data N = 4 ; maxM = 10 ; M = ceil(maxM*rand(1,N)) ; b = ceil(20*rand(maxM,N)) ; a = ceil(100*rand(1,N)) ; % your loop tmp1=0; for n=1:N tmp2=0 ; for m=1:M(n) tmp1=tmp1+1/b(m,n); end; tmp2=tmp2+a(n)*tmp1; end; R(1) = tmp2 ; % cleaned up loop tmp1=0; for n=1:N for m=1:M(n) tmp1=tmp1+1/b(m,n); end; end; R(2)=a(N)*tmp1 ; % vectorized version c = cumsum(1./b) ; R(3) = a(N)*sum(c(sub2ind(size(c),M,1:N))) ; % result disp(R) so it appeared it is not so difficult after all, isn't it? hth Jos |