How to make a program more efficient by changing for loops

2 次查看(过去 30 天)
Hi, I know this isn't really a question but I would like to learn how to make my program more efficient:
function [] = mapa
m=60;
n_re=linspace(1,6,m);
n_im=linspace(0,6,m);
z=zeros(m,m);
load('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Samples\experimental_values_M2612')
tic
en=1; %para cambiar subplot
er=2; %cambiar la linea de experimental values (wavelength)
global lambda0;
for lambda0=[450,500,520,546,568,600,632,650,694];
for v=1:18
d=Experimental_values(v,1)
Cexp=Experimental_values(v,er) %el numero depende de que columna de la tabla es:2=450nm,
%3=500nm,4=520nm,5=546nm, 6=568nm, 7=600nm, 8=632nm, 9=650nm, 10=694nm,
%HAY QUE CAMBIAR TAMBIEN LAMBDA0!!
for i=1:m
for j=1:m
n=n_re(i)+1i*n_im(j);
z(i,j)= cx2(lambda0,d,n,Cexp);
save(['Z_' num2str(v)],'z')
%--------------------------------------------------------------------------%
%--------------------------------------------------------------------------%
%guarda con un valor distinto cada vez. Tengo que borrar los
%archivos guardados si voy a hacer menos puntos (m) o si voy a
%poner menos datos (de 18 por ejemplo)!Los archivos Z_x y MeanZ y
%TotalZ.
%--------------------------------------------------------------------------%
%--------------------------------------------------------------------------%
end
toc
end
end
%dependiendo del numero de datos que tengamos tenemos que quitar algunos de
%los siguientes load, y cambiar TotalZ y el numero por el que divide MeanZ
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_1')
Z_1=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_2')
Z_2=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_3')
Z_3=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_4')
Z_4=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_5')
Z_5=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_6')
Z_6=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_7')
Z_7=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_8')
Z_8=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_9')
Z_9=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_10')
Z_10=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_11')
Z_11=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_12')
Z_12=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_13')
Z_13=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_14')
Z_14=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_15')
Z_15=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_16')
Z_16=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_17')
Z_17=z;
load ('C:\Users\Weinberg\Desktop\Prueba matlab\Pcolor_V1\Z_18')
Z_18=z;
TotalZ=Z_1+Z_2+Z_3+Z_4+Z_5+Z_6+Z_7+Z_8+Z_9+Z_10+Z_11+Z_12+Z_13+Z_14+Z_15+Z_16+Z_17+Z_18;
save ('totalZvalue','TotalZ');
MeanZ=(TotalZ/.18);
save('MeanZvalue','MeanZ');
[val,ind] = min(MeanZ(:))
[I,J] = ind2sub(size(MeanZ),ind)
nplace=[I,J]
best_n=n_re(nplace(1))+1i*n_im(nplace(2))
subplot (3,3,en)
imagesc(n_im,n_re,MeanZ);
xlabel('n_re');
ylabel('n_im');
colorbar
en=en+1;
er=er+1;
end
end
function [x2]=cx2(lambda0,d,n,Cexp)
x2=(Cexp-Cteo(lambda0,d,n)).^2;
end
function [C]=Cteo(lambda0,d,n)
load('C:\Users\Weinberg\Desktop\Prueba matlab\Prueba pcolor\nSi_nSiO2\nSi_imaginary');
load('C:\Users\Weinberg\Desktop\Prueba matlab\Prueba pcolor\nSi_nSiO2\nSi_real');
load('C:\Users\Weinberg\Desktop\Prueba matlab\Prueba pcolor\nSi_nSiO2\nSi_wavelength');
load('C:\Users\Weinberg\Desktop\Prueba matlab\Prueba pcolor\nSi_nSiO2\nSiO2_real');
load('C:\Users\Weinberg\Desktop\Prueba matlab\Prueba pcolor\nSi_nSiO2\nSiO2_wavelength');
nair=1;
dSiO2=300;
nSi_1=interp1(nSi_wavelength,nSi_real,lambda0);
nSi_2=interp1(nSi_wavelength,nSi_imaginary,lambda0);
nSi=nSi_1+(1i*nSi_2);
nSiO2=interp1(nSiO2_wavelength,nSiO2_real,lambda0);
mSiO2=Sij(nair,nSiO2)*Pj(nSiO2,dSiO2,lambda0)*Sij(nSiO2,nSi);
mflake= Sij(nair,n)*Pj(n,d,lambda0)...
*Sij(n,nSiO2)*Pj(nSiO2,dSiO2,lambda0)*Sij(nSiO2,nSi);
rSiO2=abs(mSiO2(2,1)/mSiO2(1,1))^2;
rflake=abs(mflake(2,1)/mflake(1,1))^2;
C =(rflake-rSiO2)/(rflake+rSiO2);
end
Most of the lines is trying to load some file or something to use in my program, but what I'm most worried about is the amount of for loops I have and how long each of those loops take. Is there a better way to approach what I'm doing without using so many for loops? I thank anyone that can help in advance.
Guillermo

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by