From: Lisa on
Actually, no worries, all fixed up. Thanks again!

"Lisa " <lwillats(a)unimelb.edu.au> wrote in message <hvunjk$3ok$1(a)fred.mathworks.com>...
> Thanks for your time! This is really helpful. I forgot to mention in the orginal post that 'p' is a matrix, so p(j) refers to the jth column of the matrix. I assume this will affect the last line of your code (and Roger's)?
>
> Cheers.
>
> "Matt Fig" <spamanon(a)yahoo.com> wrote in message <hvul0j$i6l$1(a)fred.mathworks.com>...
> > Alright, so I do have time. This is similar to Roger's solution except for the use of bsxfun. If you don't need the final values of d, this could be simplified even further.
> >
> >
> > Y2 = bsxfun(@times,A,r2);
> > D = SMALL_VALUE + sum(Y2,2);
> > O2 = bsxfun(@times,Y2,c'./D);
> > D = sum((bsxfun(@times,A,r2) - O).*A) + SMALL_VALUE;
> > r2 = r2 - D./p;
From: Lisa on
Thanks so much. Simulation time now down to less than 1 day!

"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hvujjk$jg1$1(a)fred.mathworks.com>...
> "Lisa " <lwillats(a)unimelb.edu.au> wrote in message <hvuged$omr$1(a)fred.mathworks.com>...
> > In my simulations these loops need to be run through almost 200000 times. With my clumsy programming I estimate that this will take almost 20 days! I would really appreciate some help in vectorising and hopefully optimising this chunck of code (below)
> >
> > Thanks!
> >
> > %Y, A and O are matrices
> > %c and r are vectors
> > %d is scalar
> >
> > for i = 1:final
> > d = SMALL_VALUE;
> > for j = 1:final
> > Y(i,j)=A(i,j)*r(j);
> > d = d + Y(i,j);
> > end
> > for j = 1:final
> > O(i,j) = (c(i)/d) * Y(i,j);
> > end
> > end
> > for j = 1:final
> > d = SMALL_VALUE;
> > for i =1:final
> > d = d + (A(i,j)*r(j) - O(i,j))* A(i,j);
> > end
> > r(j)= r(j) - d/p(j);
> > end
> - - - - - - - - - -
> Y = A.*repmat(r,final,1); % r a row vector
> O = Y.*repmat(c./(sum(Y,2)+SMALL_VALUE),1,final); % c a col. vector
> r = r - (sum((Y-O).*A,1)+SMALL_VALUE)./p; % p a row vector
>
> This assumes that r and p are row vectors and c is a column vector.
>
> Roger Stafford
From: Jan Simon on
Dear Lisa,

> %Y, A and O are matrices
> %c and r are vectors
> %d is scalar

Please post the realworld values also. It is important, if A is [10 x 10] or [1e5 x1e5].

In general: Move the vectors from the FOR loop into the array indices, e.g.:
> for j = 1:final
> Y(i,j)=A(i,j)*r(j);
> d = d + Y(i,j);
> end

"j" -> "1:final" and use elementwise operator ".*" (perhaps you must transpose [r]):
Y(i, 1:final) = A(i, 1:final) .* r(1:final);
d = d + sum(Y(i, 1:final));

Then Matlab is so cute, that you can omit even the "1:final":
Y(i, :) = A(i, :) .* r;
d = d + sum(Y(i, :));

In the next step you can replace "i" by "1:final" also, but then the matrix-vector multiplication needs a special treatment:
Y(1:final, :) = A(1:final, :) .* r(ones(1, final), :);
or with omitting the indices again:
Y = A .* r(ones(1, final), :);
If you use Matlab 7, I expect BSXFUN to be faster, but sometimes the internal Matlab interpreter accelerates the above code very well without creating the eventually large temporarily inflated r-matrix.
Y = bsxfun(@times, A, r);

Finally SUM can operate on the complete matrix A.

Good luck, Jan
From: Jakub Kopac on
also be sure you have preallocated arrays, as Y and O are formet in the for loop..
From: Lisa on
Thanks so much for this mini tutorial - it was really helpful for my understanding of Matlab code.

"Jakub Kopac" <kopac.jakubREMOVE(a)gmail.com> wrote in message <hvv9u1$9jk$1(a)fred.mathworks.com>...
> also be sure you have preallocated arrays, as Y and O are formet in the for loop..