【发布时间】:2012-08-09 12:59:04
【问题描述】:
我在 matlab 中有以下代码,非常慢。这段代码与堆栈溢出中的 previous post 有关我想知道是否有任何方法可以使 matlab 更快,当我运行代码时它应该显示图,有更新的图像,但它没有显示任何东西
%% Loading Data
cd('D:\MatlabTest\15-07SpeedSensitivity\0.3');
clear all
row=101;
column=311;
%%
%Coefficients Creation
N=5;
W = [0.005 0.10;0.10 0.20;0.20 0.30;0.30 0.40;0.40 0.50;0.50 0.60 ;0.60 0.70;0.70 0.80 ;0.80 0.90;0.90 1.0];
for ind=1:9
wn = W(ind,:);
[b,a] = butter(N,wn);
bCoeff{ind}=b;
aCoeff{ind}=a;
end
[bCoeff{10},aCoeff{10}]=butter(N,0.9,'high');
%%
%filter initialization
ZState = cell(1,10);
for i=1:10
ZState{i} = zeros(max(length(aCoeff{i}), length(aCoeff{i})) - 1, 1); %# This is the initial filter state
end
%%
bands=10;
for b=1:bands
Yout{b}{row, column}=[];
end
%%
j=1;
K = 1000:4000;
window = zeros(1,10);
figure;
y = 0; %# Preallocate memory for output
j=0;
buffSize=10;
tempMean{row,column}=[];
Gibbs=(length(K)*3)/100;
fImg{1}(row,column)=0;
%load one image
for i = 1000:length(K)
disp(i)
str = int2str(i);
str1 = strcat(str,'.mat');
load(str1);
D(:,:) = A(100:200 ,200:510);
%go throught the columns and rows
for p = 1:row
for q = 1:column
%calculte the temporal mean value based on previous ones
if(size(tempMean{p,q})<buffSize) %init the first 10
tempMean{p,q}=[D(p,q) tempMean{p,q}];
else
tempMean{p,q}=[D(p,q) tempMean{p,q}(1:end-1)];
end
if(mean2(tempMean{p,q})==0)
x=0;
else
x = double(D(p,q)/mean2(tempMean{p,q}));
end
%filtering for 10 bands, based on the previous state
for f = 1:10
[y, ZState{f}] = filter(bCoeff{f},aCoeff{f},x,ZState{f});
if(j<Gibbs)
continue;
end
if(size(Yout{f}{p,q})<10)%init the first 10 after Gibbs phenomenon
Yout{f}{p,q} = [y.^2 Yout{f}{p,q}];
else
Yout{f}{p,q} = [y.^2 Yout{f}{p,q}(1:end-1)];
fImg{f}(p,q)=mean2(Yout{f}{p,q});
end
end
end
end
if(size(fImg{1}(1,1))>1)
for k = 1:10
subplot(5,2,1);
subimage(fImg{k}*5000, [0 0.5]);
colormap jet
end
pause(0.01);
end
j=j+1;
end
disp('Done Loading...')`
【问题讨论】:
-
哇,这么多代码! SO 上的人们通常不倾向于处理需要理解几十行代码的问题,尽管也有例外。如果您发布几行您认为有问题的代码,而不是发布大量代码并要求人们找到模糊的错误/问题,您将获得更好的响应。发布短代码段的另一个好处是它允许其他用户执行本地测试。我无法获取您发布的内容并在本地运行,这让我更难为您提供帮助。
-
在 Matlab 中查找瓶颈的一个很好的工具是使用 Profiler。它会生成一个很好的报告,告诉哪些代码行需要最多时间。我建议你试试看。
标签: matlab