The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: speeding up a 'for loop' piece of code in Profiler
Replies: 3   Last Post: Aug 7, 2013 9:36 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 90
Registered: 8/28/10
speeding up a 'for loop' piece of code in Profiler
Posted: Aug 6, 2013 9:19 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

hello all,

My code is as follows:

clc;clear all

%----generate data----%
[X,Y] = meshgrid(-50:2:50, -50:2:50);
Z = X .* exp(-X.^2 - Y.^2);
X= X(:);
Y = Y(:);
Z =Z(:);
x = [X Y Z];

%----- run computation -----%
[m, M] = size (x);
dim = M - 1;

K= zeros (m,m);

for it_dim=1:dim
tmp = x(:,it_dim+1) * ones(1,m) - ones(m,1) * x(:,it_dim+1)';
tmp = tmp .* tmp;
K = K + tmp;

In the 'run computation' part, the FOR loop uses a variable 'tmp'. When I run the entire code in Profiler, the following line within the FOR loop takes the longest time :

tmp = x(:,it_dim+1) * ones(1,m) - ones(m,1) * x(:,it_dim+1)';

Can anyone suggest a way to perhaps code it more efficiently for less time consumption.


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.