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:
Concatenate alternative

Subject: Concatenate alternative

From: Brendan

Date: 26 Mar, 2010 14:17:02

Message: 1 of 5

Hi,
Could anyone tell me an alternative way of concatenating a large number of vectors that have been created in a for loop? Im using the regular method but it slows the program down significantly..
Any help would be great!

Subject: Concatenate alternative

From: us

Date: 26 Mar, 2010 14:23:05

Message: 2 of 5

"Brendan " <c_uboid88@hotmail.co.uk> wrote in message <hoifku$788$1@fred.mathworks.com>...
> Hi,
> Could anyone tell me an alternative way of concatenating a large number of vectors that have been created in a for loop? Im using the regular method but it slows the program down significantly..
> Any help would be great!

a snippet of ML code, please...

us

Subject: Concatenate alternative

From: Oleg Komarov

Date: 26 Mar, 2010 14:26:02

Message: 3 of 5

"Brendan " <c_uboid88@hotmail.co.uk> wrote in message <hoifku$788$1@fred.mathworks.com>...
> Hi,
> Could anyone tell me an alternative way of concatenating a large number of vectors that have been created in a for loop? Im using the regular method but it slows the program down significantly..
> Any help would be great!

Show us the code.
Also,what you mean by regular? --> []

Are you concatenating inside the loop? If yes this practice is discouraged, usually preallocation is preferred.

Oleg

Subject: Concatenate alternative

From: Brendan

Date: 26 Mar, 2010 14:43:05

Message: 4 of 5

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <hoig5q$g6t$1@fred.mathworks.com>...
> "Brendan " <c_uboid88@hotmail.co.uk> wrote in message <hoifku$788$1@fred.mathworks.com>...
> > Hi,
> > Could anyone tell me an alternative way of concatenating a large number of vectors that have been created in a for loop? Im using the regular method but it slows the program down significantly..
> > Any help would be great!
>
> Show us the code.
> Also,what you mean by regular? --> []
>
> Are you concatenating inside the loop? If yes this practice is discouraged, usually preallocation is preferred.
>
> Oleg
Sorry for not posting the code!! The full code(much of which is probably irrelevant but will post so it can be run) is:
y0 = [1;0;0;1];
z0 = [1.523;0;0;0.802];
w0=[1;0;0;1.09];
t0=0
tend=.01
step=tend/0.001
earth_x=[]
earth_y=[]
earth_xdot=[]
earth_ydot=[]
mars_x=[]
mars_y=[]
mars_xdot=[]
mars_ydot=[]
spacecraft_x=[]
spacecraft_y=[]
spacecraft_xdot=[]
spacecraft_ydot=[]
options=odeset('RelTol',1.e-7, 'AbsTol',1.e-9);
%for m = 1:1
[t,z]=ode45(@twobodyfunctionmars,[0:0.001:tend*0.5],z0,options);
[t,y]=ode45(@twobodyfunctionearth,[0:0.001:tend*0.5],y0,options);
[t,w]=ode45(@twobodyspacecraftfunction,[0:0.001:tend*0.5],w0,options);
%earth_x=vertcat(earth_x,y(:,1))
%earth_y=vertcat(earth_y,y(:,2))
%earth_xdot=vertcat(earth_xdot,y(:,3))
%earth_ydot=vertcat(earth_ydot,y(:,4))

mars_x=vertcat(mars_x,y(:,1))
mars_y=vertcat(mars_y,y(:,2))
mars_xdot=vertcat(mars_xdot,y(:,3))
mars_ydot=vertcat(mars_ydot,y(:,4))

spacecraft_x=vertcat(mars_x,y(:,1))
spacecraft_y=vertcat(mars_y,y(:,2))
spacecraft_xdot=vertcat(mars_xdot,y(:,3))
spacecraft_ydot=vertcat(mars_ydot,y(:,4))
rectangle('Position',[-0.25,-0.25,0.5,0.5],'Curvature',[1,1],'Facecolor','y')
daspect ([1,1,1])
ee=plot(y(:,1),y(:,2),'bo')
hold on
ss=plot(w(:,1),w(:,2), 'go')
mm=plot(z(:,1),z(:,2),'ro')
axis ([-2 2 -2 2])
grid on
drawnow
y0=[y(step*0.5,1);y(step*0.5,2);y(step*0.5,3);y(step*0.5,4)];
z0=[z(step*0.5,1);z(step*0.5,2);z(step*0.5,3);z(step*0.5,4)];
w0=[w(step*0.5,1);w(step*0.5,2);w(step*0.5,3);w(step*0.5,4)];
%end
for i=1:400
[t,z]=ode45(@twobodyfunctionmars,[0:0.001:tend],z0,options);
[t,w]=ode45(@twobodyspacecraftfunction,[0:0.001:tend],w0,options);
[t,y]=ode45(@twobodyfunctionearth,[0:0.001:tend],y0,options);
rectangle('Position',[-0.25,-0.25,0.5,0.5],'Curvature',[1,1],'Facecolor','y')
daspect ([1,1,1])
plot(y(:,1),y(:,2),'-b')
plot(w(:,1),w(:,2), '-g')
plot(z(:,1),z(:,2),'-r')
axis ([-2 2 -2 2])
grid on
%t1 = now, t2 = rem(now,1)
set(ee,'Xdata',y(:,1),'Ydata',y(:,2))
set(mm,'Xdata',z(:,1),'Ydata',z(:,2))
set(ss,'Xdata',w(:,1),'Ydata',w(:,2))
drawnow
y0=[y(step,1);y(step,2);y(step,3);y(step,4)];
z0=[z(step,1);z(step,2);z(step,3);z(step,4)];
w0=[w(step,1);w(step,2);w(step,3);w(step,4)];
earth_x=vertcat(earth_x,y(:,1))
earth_y=vertcat(earth_y,y(:,2))
earth_xdot=vertcat(earth_xdot,y(:,3))
earth_ydot=vertcat(earth_ydot,y(:,4))

mars_x=vertcat(mars_x,y(:,1))
mars_y=vertcat(mars_y,y(:,2))
mars_xdot=vertcat(mars_xdot,y(:,3))
mars_ydot=vertcat(mars_ydot,y(:,4))

spacecraft_x=vertcat(mars_x,y(:,1))
spacecraft_y=vertcat(mars_y,y(:,2))
spacecraft_xdot=vertcat(mars_xdot,y(:,3))
spacecraft_ydot=vertcat(mars_ydot,y(:,4))
end
hold off
Earth=[earth_x,earth_y,earth_xdot,earth_ydot]
Mars=[mars_x,mars_y,mars_xdot,mars_ydot]
Spacecraft=[spacecraft_x,spacecraft_y,spacecraft_xdot,spacecraft_ydot]
%max(z(:,1))+ abs(min(z(:,1)))

   the m-files for the ode's are:
twobodyfunctionmars:

function zdot=twobodymars(t,z)
rmars=sqrt(z(1)^2+z(2)^2);
zdot = [z(3);z(4);-z(1)/rmars^3;-z(2)/rmars^3;];

twobodyfunctionearth:

function ydot=twobodyEarth(t,y)
r=sqrt(y(1)^2+y(2)^2);
ydot = [y(3);y(4);-y(1)/r^3;-y(2)/r^3;];

twobodyfunctionspacecraft:

function wdot=twobodyspacecraft(t,w)
r=sqrt(w(1)^2+w(2)^2);
wdot = [w(3);w(4);-w(1)/r^3;-w(2)/r^3;];


Sorry for the long post, I just think it might be easier for you to see what Im doing with the full code. Thanks. I realise Im being quite inefficient doing all the concatenations seperately but I really dont know a way of doing it better.

Subject: Concatenate alternative

From: Máday Péter

Date: 26 Mar, 2010 17:21:58

Message: 5 of 5

Hello

Preallocating arrays could help you improve the performance.

e.g. instead of

% loop
whateverArray = vertcat(whateverArray, newValuesVector) ;

----

use

whateverArray = zeros(loopExecutionCount, newValuesVectorLength);
% loop
whateverArray(loopIdx, :) = newValuesVector;

Peter

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