how to decrease the time consumption of the code

1 view (last 30 days)
Hello I've already got a lot of help in developring the encryption technique from matlab answers. Now i have another problem. I need to reduce the time consumption of the folowing part of the code.Here i generate two chaotic sequences using Chen's hyperchaotic system. I've defined a function called Chen1.m and i've given both the codes below.This part of the code is taking almost 15 minutes.Please help.Thanks in advance.
%the values of a, b, c, d, k here
a = 36;
b = 3;
c = 28;
d = 16;
k = 0.2;
%vector v0, which is a 4x1 vector of initial conditions
v0 = [0.3 -0.4 1.2 1];
fun = @(t,v) chen1(t,v,a,b,c,d,k);
[t, v] = ode45(fun, [0 1.54], v0);
x = v(:,1);
x(257)=[];
y = v(:,2);
y(257)=[];
[lx,fx]=sort(x);
[ly,fy]=sort(y);
S=fx;
T=(fy)';
%%matrix generated for scrambling
[Tgrid, Sgrid] = meshgrid( T, S );
Z = arrayfun( @(S,T) [S T], Sgrid, Tgrid, 'UniformOutput', false );
%%scrambled matrix
F=cellfun(@(x) B{x(1),x(2)},Z,'un',0);
G=cellfun(@(x) E{x(1),x(2)},Z,'un',0);
The function is defined as follows
function vdot = chen(t, v, a, b, c, d, k)
x = v(1); y = v(2); z = v(3); q = v(4);
xdot = a*(y-x);
ydot = -(x*z)+(d*x)+(c*y)-q;
zdot = (x*y)-(b*z);
qdot = x+k;
vdot = [xdot; ydot; zdot; qdot];
end
  1 Comment
Adam
Adam on 4 Sep 2014
First thing you should do is run the profiler on it:
profile on
functionCall(...)
profile off
profile viewer
That will tell you where the bottlenecks are. One of the worst things you can do when trying to speed up code is to start focusing on really speeding up a piece of code that only takes 1% of the total running time anyway.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 4 Sep 2014
Don't use cell arrays to hold B,E, F, and G. Cell arrays are slow and unnecessary for holding arrays of scalars. Once you've repaired the damage with
B=cell2mat(B)
E=cell2mat(E)
you should also replace this code segment
Z = arrayfun( @(S,T) [S T], Sgrid, Tgrid, 'UniformOutput', false );
%%scrambled matrix
F=cellfun(@(x) B{x(1),x(2)},Z,'un',0);
G=cellfun(@(x) E{x(1),x(2)},Z,'un',0);
with this
F=B(S,T);
G=E(S,T);

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!