matlab - 使用./ Matrix尺寸必须达成一致

时间:2014-03-17 14:16:17

标签: matlab

为什么我收到此错误?

在这个程序中,我希望得到一个线性回归。接下来,我想删除异常数据。然后,异常值显示为红色,正常点显示为蓝色,拟合线显示为绿色 在此程序中,end命令发生错误。有人能帮助我理解为什么吗?

x=[-0.05623  -0.20968   0.192102  0.992332  0.576244  1.305784 ...
   -0.72931  -0.86457  -0.09492   1.383214  1.303681 -0.12581  ...
   -0.59687  -1.52065   0.671842  0.022844 -1.17779  -0.33458  ...
   -1.82946   0.42999  -0.17399   1.631242 -1.35943   0.970507 ...
    0.14364   0.082604  0.716664  1.193484 -1.07106   1.318902 ...
   -1.21     -1.07411  -0.67256   0.736462 -1.06996   0.334715 ...
    0.411883  0.15412   0.554571 -1.17285   1.007587  0.11352  ...
    0.730051 -0.98351   0.052032  0.877599  1.014141 -0.08435  ...
   -1.85353  -1.09682   0.218628  0.794246  0.463124 -0.61263  ...
    2.2444    0.072348  0.865514 -0.4157   -1.11494   0.685252 ...
    1.037673  1.822212 -0.52899  -1.62797   1.617302  0.264137 ...
   -1.12715  -0.55918  -0.80885   1.161004  0.592105  0.242748 ...
    0.240477 -0.8215    0.993112  0.346395 -0.26113  -0.18471  ...
   -0.10173  -0.88704   0.741377  1.392208  2.4739    0.503919 ...
   -0.82248   0.200982 -1.00705  -0.61317  -0.65896  -0.83323  ...
    0.378179 -1.11534   0.667241  0.795333  1.037492 -0.02043  ...
    0.618953  1.803064  0.052993 -0.17789];

y=[5.436978 3.947596 5.153861 7.203315 5.725703 6.793873 3.545316 3.936359 ...
   4.237048 6.839705 7.122279 4.810857 4.288197 2.756219 6.208018 4.655615 ...
   3.29746  4.037378 2.486194 5.55364  4.931752 7.288785 3.057518 6.765781 ...
   4.633195 4.574674 5.536068 7.039812 3.51258  6.771749 3.625891 3.429356 ...
   3.524448 5.804945 2.987637 5.782922 5.441024 4.886507 5.662118 2.994722 ...
   6.762253 5.310672 6.183994 3.647019 5.090687 6.037325 6.680508 4.631969 ...
   1.904436 3.03886  5.583692 6.479913 5.177385 4.037497 8.4407   4.928997 ...
   6.522089 4.451288 3.282642 5.863934 6.755633 7.983111 4.344549 2.487526 ...
   7.696832 5.129475 3.351763 4.571945 3.730939 6.493319 6.459757 5.291498 ...
   5.490604 3.531393 6.574259 5.65695  4.609641 4.81884  5.201688 3.25646  ...
   6.172526 6.502666 8.719206 6.323678 4.043084 5.299436 3.389938 4.235615 ...
   4.322408 3.754491 6.042199 3.087029 5.413286 5.972943 6.497619 4.859718 ...
   5.679203 8.097901 4.604103 4.588513];

n=1  

p=polyfit(x,y,n);

a=p(1); b=p(2);

ymodel=a*x+b; 

plot(x,y,'o',x,ymodel);

all_idx = 1:length(x);
outlier_idx = abs(x -median(x)) > 2*std(x) | abs(y - median(y)) > 2*std(y)

x(outlier_idx) = interp1(all_idx(~outlier_idx), ...
                         x(~outlier_idx), all_idx(outlier_idx));

y(outlier_idx) =interp1(all_idx(~outlier_idx), ...
                        y(~outlier_idx), all_idx(outlier_idx));

x_out_ind = find(outlier_idx); % gives the index of outliers

% the next line gives an error: matrix dimensions must agree:
x_normal_ind = find(ones(1,00)-x_out_ind); % gives the index of normal

for i=x_normal_ind

    % this plot command gives an error:
    plot(x(i),y(i),'o','blue')
    hold on

end

for i=x_out_ind

    % this plot command gives an error:
    plot(x(i),y(i),'o','red')
    hold on

end

plot(x,a*x+b,'green')

1 个答案:

答案 0 :(得分:1)

第一个错误是因为一个(1,00)将返回一个空矩阵,它与x_out_ind的大小不同,即矩阵尺寸不一致。

第二个错误是因为您为plot命令提供了两个字符串。这通常被视为“财产”:“价值”配对。所以它说的是'o'属性无效。

我建议使用'dbstop if error'并逐个跟踪错误。