如何优化matlab程序,这个程序运行要40多分钟,怎么优化缩短运行时间?
clearallcloseallL=128;nIter=0;iterOK=0;iter0=ones(4*L)*0.2;theta0=[0:14;15:29;30:44;4...
clear all
close all
L = 128;
nIter = 0;
iterOK = 0;
iter0 = ones(4*L)*0.2;
theta0 = [0:14; 15:29; 30:44; 45:59; 60:74; 75:89; 90:104; 105:119; 120:134; 135:149; 150:164; 165:179];
theta = reshape (theta0, 180, 1);
%theta = [0 45 90 135];
nTheta = length(theta);
I = phantom (L);
%I = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
%P = radon (I, 0:179, L);
subplot(1,2,1), imshow(I)
P = radon (I, theta, L);
P1 = radon (I, 0:30:180);
R1 = iradon (P1, 0:30:180, 'linear', 'hamming');
subplot(1,2,2), imshow(R1),
for aa = 1:L;
for bb = 1:L;
iter0 ((4*aa-3):4*aa, (4*bb-3):4*bb) = R1 (aa, bb)/16;
I1 ((4*aa-3):4*aa, (4*bb-3):4*bb) = I (aa, bb)/16;
end
end
while (nIter <= 90)%迭代次序
nIter = nIter + 1;
for ii = 1:L
proj = zeros (4*L);
proj (:,(4*ii-3):4*ii) = 1;
proj1 = imrotate (proj, theta (mod(nIter- 1, nTheta+1), 'crop'); P_proj = P (ii, mod(nIter-1, nTheta)+1);
P_proj = sum(sum(I1.*proj1));
R_proj = sum(sum(iter0.*proj1));
iter1 = iter0 + proj1.*(P_proj-R_proj)/sum(sum(proj1));
iter0 = iter1;
end
if mod(nIter,5)==0
figure, imshow(iter0);
end
end
for aa = 1:L;
for bb = 1:L;
R (aa, bb) = sum (sum (iter0 ( (4*aa-3):4*aa, (4*bb-3):4*bb)));
end
end
figure, imshow(R) 展开
close all
L = 128;
nIter = 0;
iterOK = 0;
iter0 = ones(4*L)*0.2;
theta0 = [0:14; 15:29; 30:44; 45:59; 60:74; 75:89; 90:104; 105:119; 120:134; 135:149; 150:164; 165:179];
theta = reshape (theta0, 180, 1);
%theta = [0 45 90 135];
nTheta = length(theta);
I = phantom (L);
%I = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
%P = radon (I, 0:179, L);
subplot(1,2,1), imshow(I)
P = radon (I, theta, L);
P1 = radon (I, 0:30:180);
R1 = iradon (P1, 0:30:180, 'linear', 'hamming');
subplot(1,2,2), imshow(R1),
for aa = 1:L;
for bb = 1:L;
iter0 ((4*aa-3):4*aa, (4*bb-3):4*bb) = R1 (aa, bb)/16;
I1 ((4*aa-3):4*aa, (4*bb-3):4*bb) = I (aa, bb)/16;
end
end
while (nIter <= 90)%迭代次序
nIter = nIter + 1;
for ii = 1:L
proj = zeros (4*L);
proj (:,(4*ii-3):4*ii) = 1;
proj1 = imrotate (proj, theta (mod(nIter- 1, nTheta+1), 'crop'); P_proj = P (ii, mod(nIter-1, nTheta)+1);
P_proj = sum(sum(I1.*proj1));
R_proj = sum(sum(iter0.*proj1));
iter1 = iter0 + proj1.*(P_proj-R_proj)/sum(sum(proj1));
iter0 = iter1;
end
if mod(nIter,5)==0
figure, imshow(iter0);
end
end
for aa = 1:L;
for bb = 1:L;
R (aa, bb) = sum (sum (iter0 ( (4*aa-3):4*aa, (4*bb-3):4*bb)));
end
end
figure, imshow(R) 展开
2个回答
推荐于2017-08-30
展开全部
如何优化matlab程序,这个程序运行要40多分钟,怎么优化缩短运行时间?
几个方面:
1、最大的问题是循环体里面的 pause(0.1),尤其是里面那一层循环。
2、很多赋值语句后面应该加分号,避免显示。
3、数组预置(对于这段代码而言不是很重要)。
几个方面:
1、最大的问题是循环体里面的 pause(0.1),尤其是里面那一层循环。
2、很多赋值语句后面应该加分号,避免显示。
3、数组预置(对于这段代码而言不是很重要)。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询