|
"Kurtis " <kuenrg@yahoo.com> wrote in message <i9ph31$1u9$1@fred.mathworks.com>...
> I have a nested for loop (i & j up to n) where I'm trying to perform
> for i=1:n
> for j=1:n
> P(i) += sin(x(j))*sin(y(i))
> end
> end
>
> Where P(), x() & y() are symbolic arrays and the += function operates like in C++ where P(i) takes on the value it had plus the new value.
> So if n=2 the end result would be
> P(1) = sin(x(1))sin(y(1))+sin(x(2))sin(y(1))
> P(2) = sin(x(1))sin(y(2))+sin(x(2))sin(y(2))
>
> Does that make sense?
> Is there any equivalent of the += operator for symbolic arrays?
>
> Thanks in advance!
There is no equivalent of the += in Matlab. However, you don't need it because you can vectorize this whole thing.
x= 1:10;
y = 2:2:20;
n = length(x)
P = zeros(size(y.'))
for i=1:n
for j=1:n
P(i) = P(i)+ sin(x(j))*sin(y(i))
end
end
P2 = sum(bsxfun(@(x,y)sin(x).*sin(y),x,y.'),2)
isequal(P,P2)
|