【发布时间】:2010-07-22 15:01:05
【问题描述】:
我对 matlab 还很陌生,但为了我的工作,我需要导入一个庞大的数据集并以某种方式组织它。我已经编写了一个可以做到这一点的代码,但是效率很低(这只是我的第三个主要代码,需要几个小时)。 Matlab 告诉我,我可以预先分配我的变量(实际上大约 50 次),但我无法看到如何做到这一点,因为我不确定对于 for 循环中的每次迭代,数据将被添加到哪个矩阵。代码本身可能比我更好地解释了这一点。
(这只是其中的一小部分,但希望能显示我的问题)
for x= 1:length(firstinSeq)
for y= 1:length(littledataPassed-1)
if firstinSeq(x,1)== littledataPassed(y,1) && firstinSeq(x,2)== littledataPassed(y,2)
switch firstinSeq(x,3)
case 0
for z= 0:1000
w= y+z;
if firstinSeq(x,4)== littledataPassed(w,4)
if littledataPassed(w,6)== 1 && firstinSeq(x,2)== littledataPassed(w,2) && littledataPassed(w,5)== 0
msgLength0= [msgLength0; firstinSeq(x,:) littledataPassed(w,:)];
break
else continue
end
else msgLength0= [msgLength0; firstinSeq(x,:) [0 0 0 0 0 0]];
break
end
end
case 1
for z= 0:1000
w= y+z;
if firstinSeq(x,4)== littledataPassed(w,4) %if sequence not the same, terminate
if littledataPassed(w,6)== 1 && firstinSeq(x,2)== littledataPassed(w,2) && littledataPassed(w,5)== 0
msgLength1= [msgLength1; firstinSeq(x,:) littledataPassed(w,:)];
break
else continue
end
else msgLength1= [msgLength1; firstinSeq(x,:) [0 0 1 0 0 0]];
break
end
end
case 2
for z= 0:1000
w= y+z;
if firstinSeq(x,4)== littledataPassed(w,4)
if littledataPassed(w,6)== 1 && firstinSeq(x,2)== littledataPassed(w,2) && littledataPassed(w,5)== 0
msgLength2= [msgLength2; firstinSeq(x,:) littledataPassed(w,:)];
break
else continue
end
else msgLength2= [msgLength2; firstinSeq(x,:) [0 0 2 0 0 0]];
break
end
end
for z= 0:1000
w= y+z;
if firstinSeq(x,4)== littledataPassed(w,4)
if littledataPassed(w,6)== 1 && firstinSeq(x,2)== littledataPassed(w,2) && littledataPassed(w,5)== 1
msgLength2= [msgLength2; firstinSeq(x,:) littledataPassed(w,:)];
break
else continue
end
else msgLength2= [msgLength2; firstinSeq(x,:) [0 0 2 0 1 0]];
break
end
end
关于如何预分配这些变量(msgLength0、1、2 等)的任何想法?他们没有为循环中的每个值添加数据,我不确定每次运行的最终大小。我的switch目前一共有8个case,导致这个程序很慢。
【问题讨论】:
标签: matlab