Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Compound operator with symbolic array

Subject: Compound operator with symbolic array

From: Kurtis

Date: 21 Oct, 2010 14:01:05

Message: 1 of 3

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!

Subject: Compound operator with symbolic array

From: Sean

Date: 21 Oct, 2010 14:27:03

Message: 2 of 3

"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)

Subject: Compound operator with symbolic array

From: Kurtis

Date: 22 Oct, 2010 16:18:05

Message: 3 of 3

Thanks for that reply! I've got a lot to learn about matlab that's for sure.

I needed to keep it in symbolic (string) form as long as possible but I figured something out.

Cheers!

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
>>snip
>
> 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)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us