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:
Vectorizing/Optimization

Subject: Vectorizing/Optimization

From: Lauren Gulley

Date: 10 Nov, 2010 16:33:06

Message: 1 of 2

I'm working on a senior project and I'm trying to simplify and speed up a code used to calculate the gravitational field of a group of spheres. Is it possible to vectorize/simplify this section of code?

for p = 1:length(fitted_spheres)
            
            sphere_to_field = field_coords(m,:) - fitted_spheres(p,:);
            
            % Compute acceleration generated by each fitted sphere:
            fitted_accel(p,1:3) = (-4*pi/3*fitted_radius^3*G*density*sphere_to_field)/...
                                  ((sphere_to_field(1)^2 + sphere_to_field(2)^2 + sphere_to_field(3)^2)^(3/2));

end


Thanks!

Subject: Vectorizing/Optimization

From: Sean

Date: 10 Nov, 2010 16:44:04

Message: 2 of 2

"Lauren Gulley" <laurengulley1@gmail.com> wrote in message <ibehg1$7bk$1@fred.mathworks.com>...
> I'm working on a senior project and I'm trying to simplify and speed up a code used to calculate the gravitational field of a group of spheres. Is it possible to vectorize/simplify this section of code?
>
> for p = 1:length(fitted_spheres)
>
> sphere_to_field = field_coords(m,:) - fitted_spheres(p,:);
>
> % Compute acceleration generated by each fitted sphere:
> fitted_accel(p,1:3) = (-4*pi/3*fitted_radius^3*G*density*sphere_to_field)/...
> ((sphere_to_field(1)^2 + sphere_to_field(2)^2 + sphere_to_field(3)^2)^(3/2));
>
> end
>
>
> Thanks!

doc bsxfun
to get your sphere_to_field

Then vectorize your fitted_accel computation by making it work on the whole column at once:
sphere_to_field(:,1)

etc.

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