How to replace a "for loop" to make it run faster - matlab

I have a for loop as a part of my code. Its run takes too long, specially for large lenghts of I. Is there any way to make it faster?
[temp,I,J] = unique(cc_new,'rows');
I=sort(I);
cc_new = cc_new(I,:);
C = C(I,:);
n = 0;
clear K;
for i = 1 : length(I)-1,
for j = i+1 : length(I),
if sum(intersect(cc_new(i,:),cc_new(j,:)))-sum(cc_new(j,:))==0,
n = n+1;
K(n) = j;
end
end
end
if n > 0,
K=unique(K);
removed = cc_new(K',:);
[temp,I]=setdiff(cc_new,removed,'rows');
I=sort(I);
cc_new = cc_new(I,:);
C = C(I,:);
end
cc = cc_new;

index0=find (A (:,2 )==0);
A0=A(index0,:);
A1=A;
A1(index0,:) =[]; % A ( : , 2 ) ~= 0
[ia1,ja1]=size(A1);
A20 = A1;
A30 = A20 ( : , 1 );
A40 = A0 ( : , 1 );
A50 = [ A30 ; A40 ];
[~,ix2,~] = unique(A50,'rows');
A60 = A0;
ix3 = ix2-ia1;
A60(ix3,:) = [];
A21 = A1;
A31 = A21 ( : , 2 );
A41 = A0 ( : , 1 );
A51 = [ A31 ; A41 ];
[~,ix21,~] = unique(A51,'rows');
A61 = A0;
ix31 = ix21-ia1;
A61(ix31,:) = [];
AN = [ A1 ; A60 ; A61 ];

Related

MF-DFA from Matalb to Julia implementation

I'm working with the MF-DFA method in Matlab but I need to implement it in Julia. The goal is tho obtain the Hurst exponent of the S&P 500. The matlab code is as follows:
sp500 = readtable('sp500_Nasdaq.csv','PreserveVariableNames', true) ;
spClose = table2array(sp500(:,2)) ;
SP1=cumsum(spClose - mean(spClose)) ;
SP1_ordinary=sqrt(mean(SP1.^2));
X=cumsum(spClose-mean(spClose));
X=transpose(X);
scale=[16,32,64,128,256,512,1024];
q=[-5,-3,-1,0,1,3,5];
m=1;
for ns=1:length(scale),
segments(ns)=floor(length(X)/scale(ns));
for v=1:segments(ns)
Index=( ( ( (v-1)*scale(ns) )+1):(v*scale(ns)));
C = polyfit(Index,X(Index),m) ;
fit=polyval(C,Index);
RMS{ns}(v)=sqrt(mean((X(Index)-fit).^2));
end
for nq=1:length(q),
qRMS{nq,ns}=RMS{ns}.^q(nq);
Fq(nq,ns)=mean(qRMS{nq,ns}).^(1/q(nq));
end
Fq(q==0,ns)=exp(0.5*mean(log(RMS{ns}.^2)));
end
The Julia the code looks like this:
using DelimitedFiles, TimeSeries, Plots, DelimitedFiles, Plots, StatsBase
using Polynomials, LinearAlgebra, CSV, DataFrames
sp500 = CSV.read("sp500_Nasdaq.csv", DataFrame)
sp500_V = values(sp500[:,2])
SP1 = cumsum(sp500_V .- mean(sp500_V) ) ;
SP1_Ord = sqrt(mean(SP1.^2)) ;
X = SP1 ;
X = X';
function polyfit(xVals,yVals)
n = length(xVals)
xBar, yBar = #fastmath mean(xVals), mean(yVals)
sXX, sXY = #fastmath ones(n)'*(xVals.-xBar).^2 , dot(xVals.-xBar,yVals.-yBar)
b1A = #fastmath sXY/sXX
b0A = #fastmath yBar - b1A*xBar
return b0A, b1A
end
scales = [16,32,64,128,256,512,1024];
q = [-5,-3,-1,0,1,3,5] ;
segments = zeros(Int64, (1,length(scales)))
global qRMS = zeros( length(q) ,length(scales) ) ;
global Fq = zeros( length(q) , length(scales) ) ;
#inbounds for ns = 1:length(scales)
global segments[ns] = Int(floor( length(X)/scales[ns] ) ) ;
global Index = Array{UnitRange{Int128}}(undef, (segments[ns], length(scales)) ) ;
global ft = zeros(Float64, (segments[ns], length(scales) ) ) ;
global RMS = zeros(Float64, (length(scales) ,segments[ns] ) ) ;
#inbounds for v=1:segments[ns]
global RMSk = Array{Float64}[] ;
Index = ( ( (v-1)*scales[ns] ) + 1 ):( v*scales[ns] ) ;
global C = polyfit( Index, X[Index]) ;
global p = Polynomial(C)
ft =p.(Index);
RMS[ns,v] = sqrt(mean((X[Index] .- ft).^2));
push!(RMSk,RMS )
end
#inbounds for nq = 1:length(q)
qRMS[nq,ns] = RMS[ns].^q[nq];
Fq[nq,ns] = mean( qRMS[nq,ns] ).^(1/q[nq] );
end
Fq[findall(x->x==0, q)[1], ns] = exp( 0.5*mean(log.(RMS[ns].^2) ) ) ;
end
The thing is that the array RMS in Matlab's code is an array of arrays like this:
RMS =
1×7 cell array
{1×159 double} {1×79 double} {1×39 double} {1×19 double} {1×9 double} {1×4 double} {1×2 double}
But Julia returns only the last array
RMS
7×2 Matrix{Float64}:
0.0 0.0
0.0 0.0
0.0 0.0
0.0 0.0
0.0 0.0
0.0 0.0
62178.0 18238.2
How can I obtain the same output as in Matlab?
How can you store arrays into arrays in Julia?
The solutions for this is to use RMScell = Array{Float64}[] is equivalent to a Matlab cell array
using DelimitedFiles, TimeSeries, Plots, DelimitedFiles, Plots
using Polynomials, LinearAlgebra, CSV, DataFrames, StatsBase
sp500 = CSV.read("sp500_Nasdaq.csv", DataFrame) ;
sp500_V = values(sp500[:,2]) ;
SP1 = cumsum( sp500_V .- mean(sp500_V) ) ;
SP1_Ord = sqrt( mean(SP1.^2) ) ;
X = SP1 ;
X = X' ;
function polyfit(xVals,yVals)
n = length(xVals)
xBar, yBar = #fastmath mean(xVals), mean(yVals)
sXX, sXY = #fastmath ones(n)'*(xVals.-xBar).^2 , dot(xVals.- xBar,yVals.-yBar)
b1A = #fastmath sXY/sXX
b0A = #fastmath yBar - b1A*xBar
return b0A, b1A
end
""" Multifractal detrended fluctuation analysis of time series """
scales = [16,32,64,128,256,512,1024];
q = [-5,-3,-1,0,1,3,5] ;
segments = zeros(Int64, (1,length(scales))) ;
global qRMS = zeros( length(q) ,length(scales) ) ;
global Fq = zeros( length(q) , length(scales) ) ;
global RMScell = Array{Float64}[] ;
global qRMScell =[] ;
global segmentsFq = [] ;
#inbounds for ns = 1:length(scales)
global segments[ns] = Int(floor( length(X)/scales[ns] ) ) ;
global ft = zeros(Float64, (segments[ns], length(scales) ) ) ;
global RMS = zeros(Float64, segments[ns]);
#inbounds for v=1:segments[ns]
global Index = ( (v-1)*scales[ns] ) + 1: v*scales[ns] ;
global C = polyfit( Index, X[Index]) ;
global p = Polynomial(C) ;
ft =p.(Index) ;
RMS[v] = sqrt(mean((X[Index] .- ft).^2)) ;
end
l = deepcopy(RMS)
push!(RMScell,l)
global IndexFq = ((ns-1)*length(q) ) + 1 : ns*length(q) ;
push!(segmentsFq, IndexFq) ;
#inbounds for nq = 1:length(q)
l = RMScell[ns].^q[nq]
r = deepcopy(l) ;
push!(qRMScell, r) ;
end
#inbounds for nq = 1: length(scales)
Fq[nq,ns] = mean( qRMScell[segmentsFq[ns]][nq] ).^(1/q[nq] ) ;
end
Fq[findall(x->x==0, q)[1], ns] = exp( 0.5*mean(log.(RMScell[ns].^2) ) ) ;
end
Hq = zeros( Float64,length(q) ) ;
global qRegLine = Array{Float64}[] ;
for nq = 1:length(q)
global C = polyfit( log2.(scales),log2.(Fq[nq,:]) ) ;
Hq[nq] = C[2] ;
global p = Polynomial(C) ;
push!( qRegLine, p.( log2.(scales) ) )
end
tq = Hq.*q .- 1 ;
hq = diff(tq)./(q[2]-q[1]) ;
Dq = ( q[1:end-1].*hq ) - tq[1:end-1] ;

the script works normally for few data but if it accepts a matrix with a lot of data it takes too long time

That script(matlab) works normally and fast for few data but if it accepts a matrix with a lot of data it takes too long time >1hr it run with no error but very very slow. What is the reason and what i can do for that (what are the main factors that make a program slow)
clear;clc;close all;
pref=input('Please enter file prefix: ','s');
fn=[pref '.mhk'];
fr=fopen(fn,'r');
k=0;
error = false;
t = fgetl(fr);
XTH = [];
Yed = [];
while ~feof(fr)
t = fgetl(fr);
[temp,~]=sscanf(t,'%f');
if ~error
k = k + 1;
XTH(k) = temp(1);
Yed(k) = temp(2);
end
end
x = [XTH(1)];
y = [Yed(1)];
XTH=[x XTH];
Yed = transpose([y Yed]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n = length(XTH);
l = 0;
k = 0;
Vmin = 100000000;
r=0;
for u1 = 1:XTH(end)
l = l + 1;
if ismember(u1,XTH)==1
u1 = u1 + 0.1;
end
if u1>=XTH(1)+1 && u1< XTH(end)-1
for u2 = u1:XTH(end)
k = k + 1;
if u2 == u1
u2 = u2 + 1;
end
if ismember(u2,XTH)
u2 = u2 + 1;
end
XTH = [(XTH) u1 u2];
XTH = sort(XTH);
num1 = find(XTH==u1);
num2 = find(XTH==u2);
XTH = XTH(XTH~=u1);
XTH = XTH(XTH~=u2);
a11 = XTH(1:num1);
a12 = repelem(u1,length(XTH)-num1);
a1 = transpose([a11, a12]);
a2 = ones(n,1);
a31 = transpose(zeros(num1,1));
a32 = XTH(num1+1:num2-1)-u1;
a33 = repelem(u2-u1,length(XTH(num2:end)));
a3 = transpose([a31 a32 a33]);
a41 = transpose(zeros(num2-1,1));
a42 = XTH(num2:end)-u2;
a4 = transpose([a41 a42]);
A = [a1 a2 a3 a4];
AtA = transpose(A)*A;
B = Yed;
AtB = transpose(A)*B;
if det(AtA)==0
continue
end
if det(AtA)<1e-3
continue
end
solution = linsolve(AtA, AtB);
alpha1 = solution(1,1);
beta1 = solution(2,1);
alpha2 = solution(3,1);
alpha3 = solution(4,1);
beta2 = (alpha1*u1) + beta1 - (alpha2*u1);
beta3 = (alpha1*u1) + beta1 + alpha2*(u2-u1) - (alpha3*u2);
dy1 = 0;
dy2 = 0;
dy3 = 0;
Dx = XTH(end)-XTH(1);
width = 10;
for j = 1:num1
dy1 = dy1 + abs((Yed(j)-(alpha1*XTH(j)+beta1)));
end
for j = num1:num2-1
dy2 = dy2 + abs((Yed(j) - (alpha2*XTH(j)+beta2)));
end
for j = num2:n
dy3 = dy3 + abs((Yed(j) - (alpha2*XTH(j)+beta2)));
end
V(l+k) = Dx*width*(1/length(Yed))*(dy1+dy2+dy3);
if V(l+k) < Vmin && V(l+k)~=0
aa1 = alpha1;
aa2 = alpha2;
aa3 = alpha3;
bb1 = beta1;
bb2 = beta2;
bb3 = beta3;
U1 = u1;
U2 = u2;
Vmin = V(l+k);
end
end
end
end
plot(transpose(XTH), Yed, 'k')
daspect([10,1,1]);
% ax = gca;
% ax.XLim = [XTH(1) XTH(end)];
hold on;
grid on;
x11 = XTH(1);
x21 = U1;
y11 = aa1*x11 + bb1;
y21 = aa1*x21 + bb1;
plot([x11,x21],[y11,y21], 'r')
x21 = U1;
x22 = U2;
y21 = aa2*x21 + bb2;
y22 = aa2*x22 + bb2;
plot([x21,x22],[y21,y22], 'r')
x31 = U2;
x32 = XTH(end);
y31 = aa3*x31 + bb3;
y32 = aa3*x32 + bb3;
plot([x31,x32],[y31,y32], 'r')
%line(U1,aa1*U1 + bb1)
disp('Line equation 1:')
line1 = ['y = ', num2str(aa1),'*x + ',num2str(bb1)];
disp(line1)
disp('Line equation 2:')
line2 = ['y = ', num2str(aa2),'*x + ',num2str(bb2)];
disp(line2)
line3 = ['y = ', num2str(aa3),'*x + ',num2str(bb3)];
disp(line3);
vol = [num2str(Vmin),' m^3'];
disp('Minimum volume achieved:')
disp(vol)

about Clamped cubic spline code in Matlab

I did the program, but for some reason it marks a mistake (c = tridiagonal ( hi(1:n-1), dd, hi(1:n-1), ri ); ) and I don't know why, I'm sure it's correct so please somebody help.
Here is the code:
function csc = cubic_clamped ( xi, fi, fpa, fpb )n = length ( xi );
m = length ( fi );
if ( n ~= m )
disp ( 'number of ordinates and number of function values must be equal' )
return
end
for i = 1 : n-1
hi(i) = xi(i+1) - xi(i);
end
dd(1) = 2.0*hi(1); dd(n) = 2.0*hi(n-1);
ri(1) = (3.0/hi(1))*(fi(2)-fi(1)) - 3.0 * fpa;
ri(n) = 3.0 * fpb - (3.0/hi(n-1))*(fi(n)-fi(n-1));
for i = 1 : n-2
dd(i+1) = 2.0 * ( hi(i) + hi(i+1) );
ri(i+1) = (3.0/hi(i+1))*(fi(i+2)-fi(i+1))-(3.0/hi(i))*(fi(i+1)-fi(i));
end
disp ( [dd' ri'] )
c = tridiagonal ( hi(1:n-1), dd, hi(1:n-1), ri );
d = zeros ( n,1 );
b = d;
for i = 1 : n-1
d(i) = (c(i+1)-c(i))/(3.0*hi(i));
b(i) = (fi(i+1)-fi(i))/hi(i) - hi(i)*(c(i+1)+2.0*c(i))/3.0;
end
if ( nargout == 0 )
disp ( [ xi' fi' b c' d ] )
else
csc = [ xi' fi' b c' d ];
end
tridiagonal code (error is: w = a(i) - b(i)*v(i-1); )
function y = tridiagonal( a, b, c, f )
n = length(f);
v = zeros(n,1);
y = v;
w = a(1);
y(1) = f(1)/w;
for i=2:n
v(i-1) = c(i-1)/w;
w = a(i) - b(i)*v(i-1);
y(i) = ( f(i) - b(i)*y(i-1) )/w;
end
for j=n-1:-1:1
y(j) = y(j) - v(j)*y(j+1);
end

Simulate a custom function in Matlab

I'd like to simulate the following function:
l(t) = mu + Σ (1 + (t-t_i)/alpha)^(beta)
I wrote the function as follow:
function[l] = custom(m,t,H,par)
k = length(H);
n = length(t);
l = par.mu(m)*ones(n,1);
for i = 1:n
for j = 1:k
h = H{j};
h = h(h < t(i));
if ~isempty(h)
d = t(i) - h;
l(i) = l(i) + sum((1+ (d/par.alpha(m,j)))^ par.beta(m,j));
end
end
end
Now, how can I simulate this function for t=1000, par.mu= 0.5, par.alpha = 0.1, par.beta = 0.3?
I have the following code for simulation of this function but it's not efficient...How can I make it better?
function[h] = simcustom(t,par)
h = -log(rand)/par.mu;
if h < t
do5 = 1;
i = 0;
j = 0;
k = 0;
n = 1;
while true
if do5;
k = k + 1;
Lstar(k) = custom(1,h(n),{h},par); %#ok
end
j = j + 1;
U(j) = rand; %#ok
i = i + 1;
u(i) = -log(U(j))/Lstar(k); %#ok
if i == 1
Tstar(i) = u(i); %#ok
else
Tstar(i) = Tstar(i-1) + u(i);
end
if Tstar(i) > t
h = {h};
break
end
j = j + 1;
U(j) = rand;
if U(j) <= custom(1,Tstar(i),{h},par)/Lstar(k)
n = n + 1;
h = [h Tstar(i)];
do5 = true;
else
k = k + 1;
Lstar(k) = custom(1,Tstar(i),{h},par);
do5 = false;
end
end
else
h = {[]};
end
And here is the application:
par2.mu = 0.5;
par2.alpha = 0.1;
par2.beta = 0.3;
t = 1000;
h2 = simcustom(t,par2);

inputing multiple variables into a for loop in matlab

When i'm running this code in matlab, it is printing the g4 value into the array columns were g3 and its calculated value is suppose to be, as well as its own column. I was just wondering how to stop g4 being placed into g3's column and instead, print g3 and its value in the two arrays.
Cheers
v_meas = 0;
g1 = 1.09;
g2 = 0.9;
g3 = 0.93;
g4 = 0.85;
radius = 3.75;
K = 0.006;
m = g1;
g = g3;
for ii = 1 : 1 : 2
v_meas = m*((radius^2)*pi)*K;
ArrayOfDarceys(1,ii) = v_meas;
ArrayOfGradients(1,ii) = m;
v_meas = 0;
m = g2;
for jj = 3 : 1 : 4
v_meas = g*((radius^2)*pi)*K;
ArrayOfDarceys(1,jj) = v_meas;
ArrayOfGradients(1,jj) = g;
v_meas = 0;
g = g4;
end
end
ArrayOfDarceys
ArrayOfGradients
I suspect you didn't intend to nest your for loops. Try this:
for ii = 1 : 1: 2
v_meas = m*((radius^2)*pi)*K;
ArrayOfDarceys(1,ii) = v_meas;
ArrayOfGradients(1,ii) = m;
v_meas = 0;
m = g2;
end
for jj = 3 : 1: 4
v_meas = g*((radius^2)*pi)*K;
ArrayOfDarceys(1,jj) = v_meas;
ArrayOfGradients(1,jj) = g;
v_meas = 0;
g = g4;
end
If I understand what you are trying to do you could significantly simplify your code though. There is, in fact, no need for any for loops:
ArrayofGradients = [1.09,0.9,0.93,0.85]
ArrayofDarceys = ArrayofGradients*((radius^2)*pi)*K