我是相當新的matlab,但對於我的工作,我需要導入一個ENORMOUS數據集並以某種方式進行組織。我寫了一個代碼,可以做到這一點,但非常不方便(這只是我的第三大代碼,需要幾個小時)。 Matlab告訴我,我可以預先分配我的變量(事實上大約五十次),但我無法看到如何做到這一點,因爲我不確定在for循環中每次迭代將爲數據添加什麼矩陣。代碼本身可能比我更好地解釋了這一點。
(這只是一小塊,但有望顯示我的問題)麻煩在Matlab預分配一個複雜的循環
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等)有什麼想法?他們沒有爲循環中的每個值添加數據,我不確定每次運行的最終大小。現在我的轉換總共有八個案例,這使得這個程序非常緩慢。
感謝您的回覆。它是一個研究項目的整個數據集。大約30,000行的210個文本文件和大約80,000行的210個文本文件正在被讀取(它們成對出現)到Matlab中。我試圖設置我的腳本的方式是讀取一對文件,匹配相應的數據行,然後在我想要處理的行上運行這個荒謬的循環。 〜200,000行數據由該循環處理(firstinSeq varaible)。所以要回答你的問題,導入,然後排序。我會發布我的整個腳本給你,但它是非常不友好的,需要600行。 – Maxwell 2010-07-22 16:38:57
@Maxwell:所以如果你拿一對匹配的文件,在一場比賽中做30,000行1:〜3,其他80,000行?那麼你是否試圖在Matlab中構建一個存儲來自兩者的聚合數據的數組?對於這種數據纏繞,在開始嘗試將數據讀入Matlab之前,通常儘可能使用諸如sed和awk之類的實用程序,以便在沒有其他原因的情況下獲得性能。 – 2010-07-22 17:59:40
好吧,所以自從第一篇文章發佈以來,我一直在玩這個代碼,並對它進行了大量修改。我已經最終想出瞭如何預先分配這個。代碼仍然很慢,因爲我真的不知道如何簡化/向量化這個循環,但有很大的改進。 – Maxwell 2010-07-22 18:58:38