2011-04-03 130 views
0

Matlab給我錯誤,「下標賦值尺寸不匹配」,但我認爲不應該有問題。代碼如下,但基本上我有一個臨時矩陣模仿另一個矩陣testData(實際上是它的一個子集)的維數。我可以將imread的輸出分配給臨時矩陣,但不能分配給具有相同維度的testData的子集。我甚至可以使用尺寸函數來證明它們是相同的尺寸,但一個可以工作,一個不可以。所以我設置temp = imread,然後testData = temp並且它工作。但爲什麼我必須這樣做呢?可能不正確的Matlab錯誤:「下標賦值尺寸不匹配」

 
fileNames = dir('Testing\*.pgm'); 
numFiles = size(fileNames, 1); 
testData = zeros(32257, numFiles); 
temp = zeros(32256, 1); 

for i = 1 : numFiles, 
    fileName = fileNames(i).name; 

    % Extracts some info from the file's name and stores it in the first row 
    testData(1, i) = str2double(fileName(6:7)); 

    % Here temp has the same dimensions as testData(2:end, i) 
    % yet testData(2:end, i) = imread(fileName) doesn't work 
    % however it works if I use temp as a "middleman" variable 
    temp(:) = imread(fileName); 
    testData(2:end, i) = temp(:); 
end 

回答

0

如果您正在閱讀的文件是彩色圖像,imread返回MxNx3陣列。即使它包含相同數量的元素,也不能將3D數組指定給1D矢量,而無需重新定型。這可能是您嘗試將imread的輸出直接分配給testData時出現錯誤的原因。但是,當您使用中間變量並將其摺疊到列向量中時,分配將起作用,因爲現在您將1D向量分配給另一個大小相等的向量1D

如果你不想使用一個額外的步驟,試試這個

testData(2:end,i)=reshape(imread(fileName),32256,1); 
+0

它實際上是灰度,但重塑事工作正常。謝謝。 – BluePlateSpecial 2011-04-08 03:24:15