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:
Vectorization

Subject: Vectorization

From: pmassicotte

Date: 13 Jun, 2012 17:52:18

Message: 1 of 5

Hi everyone.

I'm trying to improve some of my code using vectorization. For
instance, I was using nested loop to calculate this very simple
equation:

http://s7.postimage.org/4izfctq0b/Untitled.png

where N is a 2d matrix and i and j are line and colon.

This is much be pretty straightforward, but I can't find how to do
this using vectorization.

Any help would be greatfull.

Regards,
Phil

Subject: Vectorization

From: Matt J

Date: 13 Jun, 2012 17:58:08

Message: 2 of 5

pmassicotte <pmassicotte@hotmail.com> wrote in message <eea3cdc8-9e00-418a-8401-f781c924f942@eh4g2000vbb.googlegroups.com>...
>
> http://s7.postimage.org/4izfctq0b/Untitled.png
>
> where N is a 2d matrix and i and j are line and colon.
>
> This is much be pretty straightforward, but I can't find how to do
> this using vectorization.
===========

norm(N(:))^2

Subject: Vectorization

From: pmassicotte

Date: 13 Jun, 2012 18:20:57

Message: 3 of 5

On Jun 13, 1:58

Subject: Vectorization

From: Matt J

Date: 13 Jun, 2012 19:47:07

Message: 4 of 5

pmassicotte <pmassicotte@hotmail.com> wrote in message <1d2e2840-9bb3-4bed-8ab9-5b045b58409a@m10g2000vbn.googlegroups.com>...
> On Jun 13, 1:58

Thank you, it works fine.

Let's say I have a bit more complex example like this.

http://s17.postimage.org/kag5nq5vj/Untitled.png

Do I have to look for bsxfun or something like this?
=====================

Your posts aren't displaying fully in MATLAB Central. I've copied your last post from Google groups. Yes, BSXFUN would be what is needed

J=1:m;
I=J.';
result=bsxfun(@minus,I,J).^2.*N;
result=sum(result(:));

Subject: Vectorization

From: pmassicotte

Date: 14 Jun, 2012 12:11:56

Message: 5 of 5

On Jun 13, 3:47

Tags for this Thread

No tags are associated with 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