MATLAB help optimize code - matlab

Currently I am writing a MATLAB program, which generates a fractal, based off of user-defined inputs. The problem is that when I run this piece of code with large arrays it takes an enormous amount of time to process. I am looking for advice or suggestions on how to optimize this code. Any responses will be greatly appreciated.
The main program is
for j=1:1:rows
for k=1:1:rows
Z(j,k)=iter(Z(j,k),c1(j,k),niter,f1);%Z and c1 are complex number,niter is an integer f1 is a handle
end
end
The function iter is
function z=iter(z1,c,niter,f1)
for i=1:1:niter
z1=f1(z1,c);
if abs(z1)<=2
z=i;
else
z=i;
break;
end
end
end

Related

Adjusting STATA id and t columns with a user-friendly matlab function

Here is a piece of matlab code that seems very peculiar. For some time flow reasons, sorting id and t columns the data thoroughly makes stata working almost faster than Excel usage.
function output = stataenterance(N,T)
% generating id and t columns in stata
% helping stata data entrance
time=(1:T)';
t=repmat(time,N,1);
for i = 1:N
x=i*ones(T,1);
end
idc=cell(N,1);
for i=1:N
for j=1:N
idc{j} = repmat(j,T,1);
end
end
id=cell2mat(idc);
output=[id t];
Is there any alternative way do you know faster than this code? Thanks a lot :D

Tips for speeding up douple-loop integral

I currently have the following code:
function v3matrix1 = v3(l,eigendata,sphere)
l0=l;
v3matrix1=zeros(2*l0+1,2*l0+1);
for m0=-l0:1:l0
for m0p=-l0:1:l0
fun=#(theta,phi)conj(Ylm(l0,m0,theta,phi)).*(-gamma11(1,eigendata,sphere,theta,phi)).*partialTheta2(l0,m0p,theta,phi).*sin(theta);
v3matrix1(m0+l+1,m0p+l+1)=integral2(fun,0,pi,0,2*pi);
end
end
end
It takes ~21 mins to run. I have tried to follow similar advice to that given here (Speeding up a double loop over integrals with changing parameter function values in matlab) by getting rid of the for loops using "arrayfun" and "meshgrid" but that didn't speed it up. I was wondering if anyone has any other tips or suggestions that might speed this code up?
Thank you very much.

Matlab -Comparing Values in a Matrix Without using loops in Matlab

As you know, using loops with big size matrix results delay, in my case I need to free my code of loops for the same reason(delay). This is my code example:
for i=1:n
for j=1:m-1
Z=A(i,j);
Z2= A(i,j+1);
if X(Z,Z) == X(Z2,Z)
X(Z2,Z) = X(Z2,Z)+1;
end
end
end
So, are there any suggestion? Many thanks.

matlab timer functions

I want to call function A in a program. This function has a for loop inside and I'd like to shorten amount of iterations but not just changing the upper limit. How could I do it in this case? I was thinking if Matlab is capable of doing something like: one timer inside a function (or maybe inside a loop) and second in the main program that calls this function? But only rough idea, I'm a beginner. Please feed back if this is good idea and how could it be implemented?
thank you!
It sounds like you're talking about having a maximum elapsed time condition in your loop, something along the lines of,
MAX_T = 10;
tic;
for n=1:NMAX
% Call your loop functions
.
.
% Break if youve spent too much time in the loop
if toc > MAX_T; break; end;
end
There are also ways of optimizing this, such as only checking the value of toc every N iterations.

MATLAB vectorize nested for loop

Hi I am struggling with matlab vactorization any help on this would be great thanks.
oldLocation, Limit_ are both matrices
for i=1:250
for j=1:350
temp= oldLocation(i,j,:)./Limit_(i,j,:);
end
end
Just perform the division directly. Of course, you'll need to adapt the code that follows (unless you keep overwriting temp)
temp = oldLocation./Limit_;