matlab错误"索引超出矩阵维度"具有特定数据文件

时间:2016-11-30 18:31:54

标签: matlab loops indexing

我将以下代码用于不同的数据文件,没有任何问题。但是,有一个文件会产生麻烦,当我用该文件运行代码时,我收到一个错误:

'指数超出矩阵维度。'

我认为这是因为'我'等于2546,但是当代码运行行i=i+1时,它不是在2546处停止,而是在2547继续前进和停止(给出错误) - 这当然超过了矩阵尺寸。事实上,当代码停止工作,产生错误时,我可以在工作区中看到' i'等于2547,' j'到2(而不是5,如果循环工作正常)。

由于完全相同的代码与其他文件完全兼容,我假设与此特定文件有关。有关如何解决问题的任何见解?

以下是代码:

for i=1:size(colInd,1)
    for j=1:size(colInd,2)
        if colInd(i,j)>0 && colInd(i,j)<=13
            M1(i,j)=Windowsdata(i,colInd(i,j));
        elseif colInd(i,j)==0 | colInd(i,j)==14
            M1(i,j)=NaN;
        elseif colInd(i,j)==-1 | colInd(i,j)==15
            M1(i,:)=NaN;
            i=i+1;
        end
    end
end

来自colInd的示例行,即2546 x 5 double

 4  5   6   7   8
-1  0   1   2   3
 2  3   4   5   6
11  12  13  14  15
 0  1   2   3   4
 5  6   7   8   9
 3  4   5   6   7
 5  6   7   8   9
-1  0   1   2   3
11  12  13  14  15

来自Windowsdata的示例行,即2546 x 13 double

 -4.37370443344116  -1.64714550971985   0.569347918033600   1.62668454647064    3.73541021347046    5.15196514129639    4.04361486434937    1.77491927146912    0.702701866626740   -0.354207783937454  1.18695282936096    2.82701897621155    4.01644039154053
  3.72757863998413  1.44241857528687    -1.15181946754456   -2.97936320304871   -5.16328191757202   -4.25508642196655   -2.47518587112427   0.287524074316025   -1.17596077919006   -2.04023623466492   -2.78539514541626   -2.96725606918335   -5.59557294845581
 -5.52127933502197  -1.69257545471191   3.61181259155273    4.46472501754761    0.345008432865143   -4.78608989715576   -7.80892658233643   -8.83082866668701   -5.61083126068115   -4.40270948410034   -3.05102157592773   -4.67261123657227   -5.50971889495850
  1.24733197689056  0.692575275897980   0.549045324325562   1.33569169044495    2.26527953147888    3.19271230697632    1.92626762390137    -0.00543282041326165    -1.76812970638275   -3.55482935905457   -2.28071475028992   2.58129334449768    6.07476711273193
  2.17950797080994  2.73428583145142    1.63492679595947    -0.256836771965027  -0.773400425910950  -1.04227805137634   -1.82435607910156   -2.64025163650513   -1.53338134288788   -2.29410648345947   -4.26442241668701   -4.76120758056641   -4.47712421417236
 -0.246993020176888 0.157185763120651   0.250829964876175   -0.986824631690979  1.40918886661530    5.03370332717896    8.15515422821045    6.41663646697998    2.43448591232300    -2.98093175888062   -3.53510475158691   -1.89243125915527   1.47953033447266
  4.36318445205688  5.06837177276611    5.78645181655884    6.97499608993530    7.49895095825195    5.27076244354248    4.75153970718384    4.35132837295532    2.37539553642273    0.0745598822832108  0.782306909561157   1.98255372047424    1.82295107841492
  0.393009424209595 0.348423480987549   -0.0242169145494699 -0.451373100280762  0.792472958564758   3.95410203933716    6.95971775054932    6.07247447967529    4.61793804168701    2.25326156616211    1.17793440818787    -1.02191674709320   -1.40514099597931
  2.97367334365845  2.56695508956909    -0.0324615947902203 -0.512259364128113  -0.169182881712914  1.99416732788086    2.05820631980896    1.26427924633026    -0.107465483248234  -1.26579785346985   -2.51656532287598   -2.19553661346436   -1.86673855781555
 -5.92374515533447  -4.78130531311035   -5.02523994445801   -4.12971973419189   -2.56698751449585   -2.16855669021606   -2.66882371902466   -3.24165868759155   -4.10617780685425   -4.71752023696899   -4.63748264312744   -3.33325529098511   -2.00388121604919

1 个答案:

答案 0 :(得分:0)

如果我理解正确,colInd是一个NxM矩阵,N=2546并且你有期望,在你的最后一个地方,通过将i增加一个你最后进入下一个外部for循环迭代,在j次迭代中从1开始M(i+1)th

如果这是您想要实现的行为,则需要使用break语句来打破内部for循环。否则,如果j<size(colInd,2)j将递增并且内循环继续。

如果我正确理解了您想要的行为,那么这不是文件的问题,而是您的算法问题。但正如@excaza指出的那样,你应该给出一个mcve的例子,包括必要的变量(如Windowsdata)。否则,很难理解你想要做的事情。

编辑:尝试一下:

for i=1:size(colInd,1)
    for j=1:size(colInd,2)
        if colInd(i,j)>0 && colInd(i,j)<=13
            M1(i,j)=Windowsdata(i,colInd(i,j));
        elseif colInd(i,j)==0 | colInd(i,j)==14
            M1(i,j)=NaN;
        elseif colInd(i,j)==-1 | colInd(i,j)==15
            M1(i,:)=NaN;
            break;
        end
    end
end

编辑2:澄清:如果在i=size(colInd,1)(即i=2546)和j<size(colInd,2)(即j<5的迭代中,我们假设{{1为简单起见)最后j=1成立。因此,导致elseif递增到i,内循环将经历下一次迭代。现在使用i=2546+1=2547,如果您尝试访问超出j=2维度的colInd(2547,2),则会在第一个colInd

Edit3:如果你想要更多的Matlab-y实现,因为for循环不是很好的Matlab编码风格,我还附加了这个使用矢量化的解决方案(尽管不是那么好)。

colInd = [ 4  5   6   7   8;
-1  0   1   2   3;
 2  3   4   5   6;
11  12  13  14  15;
 0  1   2   3   4;
 5  6   7   8   9;
 3  4   5   6   7;
 5  6   7   8   9;
-1  0   1   2   3;
11  12  13  14  15;
10  11  12  13  14];

Windowsdata = reshape([1:size(colInd,1)*13],[size(colInd,1) 13]);

M1 = zeros(size(colInd));
M2 = zeros(size(colInd));

c1 = find(colInd>0&colInd<=13);
c2 = find(colInd==0|colInd==14);
c3 = find(colInd==-1|colInd==15);
[x1,~] = ind2sub(size(colInd),c1);
[x3,~] = ind2sub(size(colInd),c3);

M2(c1) = Windowsdata(sub2ind(size(Windowsdata),x1,colInd(c1)));
M2(c2) = NaN;
M2(x3,:) = NaN;

它的运行速度大约是你的for循环实现的3-5倍。

编辑:在sub2ind调用中添加了缺少的术语,它提供了与for循环相同的结果:

M1 =
 34    45    56    67    78
NaN   NaN   NaN   NaN   NaN
 14    25    36    47    58
NaN   NaN   NaN   NaN   NaN
NaN     5    16    27    38
 50    61    72    83    94
 29    40    51    62    73
 52    63    74    85    96
NaN   NaN   NaN   NaN   NaN
NaN   NaN   NaN   NaN   NaN
110   121   132   143   NaN

M2 =
 34    45    56    67    78
NaN   NaN   NaN   NaN   NaN
 14    25    36    47    58
NaN   NaN   NaN   NaN   NaN
NaN     5    16    27    38
 50    61    72    83    94
 29    40    51    62    73
 52    63    74    85    96
NaN   NaN   NaN   NaN   NaN
NaN   NaN   NaN   NaN   NaN
110   121   132   143   NaN