0

给定以下代码:

clc
clear all
P = 45;

Mat =[
   64.7331   62.4019   60.2700   58.7798   58.4334   62.6596   59.4961   56.2320   53.6037 52.9330   60.9245   56.7753   51.7143   46.3398   44.5793   59.9245   55.0268   48.0563 36.3834   27.3842   59.9858   55.1389   48.3194   37.4449   30.2616
   57.9874   54.0479   50.6911   50.0354   52.6935   55.4367   49.3499   41.7788   39.6147 46.7394   54.3272   46.7781   32.1549   20.1516   42.9207   55.3253   49.1113   41.1552 38.7894   46.4079   57.8245   53.7807   50.2828   49.5916   52.3766
   63.0248   62.0303   61.8372   62.5024   63.8418   59.4631   57.8597   57.5336   58.6360 60.6997   55.2480   52.3275   51.6672   53.8008   57.2342   50.6163   44.3727   42.4670 47.8379   53.8624   47.3455   34.0518   22.7165   42.7145   51.8266
   60.1421   55.2778   48.3022   35.9256   16.6506   60.8750   56.5697   51.0854   44.5373 41.5090   62.4192   59.0475   55.4089   52.2317   51.1936   64.3903   61.8774   59.4945 57.7313   57.2225   66.4892   64.6241   63.0031   61.9060   61.6064
   55.8668   54.2772   54.7708    7.0326   59.9984   50.3209   46.7794   47.9743   52.4927 57.1425   44.2363   32.9105   38.0425   48.3069   55.0584   42.9081   25.1741   34.8503 47.5309   54.7311   48.3741   43.5202   45.2490   51.0490   56.3626
   62.8081   62.7123   63.3845   64.6575   66.2861   58.8319    4.6746   59.7547   61.6696 63.9329   53.6053   53.3021   55.2850   58.3709   61.5730   46.1003   45.3092   49.8225 55.0705   59.5074   34.5594   30.5746   44.5462   52.7418   58.2459
   59.7305   56.9737   55.1115   55.0603   56.8513   56.5791   52.0789   48.2510   48.1309 51.8531   54.0783   47.0293   37.1612   36.6764   46.6022   53.4451   45.4219   29.5077 28.1924   44.8975   55.1133   49.3157   43.1930   42.9668   48.9959
   44.4319   46.3901   51.8259   56.8807   61.0151   26.7070   36.7384   48.2747   55.1761 60.0372   30.2115   37.9156   48.5729   55.3044   60.1077   45.7796   47.4661   52.3927 57.1888   61.2012   53.6748   54.3513   56.8248   59.9279   62.9754];


for j = 1:1:size(Mat,2)

      if isnan(Mat)
        break;
      end

        for matrows=1:1:size(Mat,1)
        [ski1,snow1]=min(Mat(:,j));

        if ski1 <=P
            M_B = snow1;
            Mat(snow1,:) = NaN;            
%             Mat
            [ski2,snow2]=min(Mat(:,j));
        else
            break
        end
                if ski2 + ski1 <=P
                    M_B = horzcat(snow1,snow2);
                    Mat(snow2,:) = NaN;
        %           Mat
                    [ski3,snow3]=min(Mat(:,j));
                else
                    break
                end

                    if ski3 + ski2 + ski1 <=P
                        M_B = horzcat(snow1,snow2,snow3);
                        Mat(snow3,:) = NaN;
            %           Mat
                        [ski4,snow4]=min(Mat(:,j));
                    else
                        break
                    end


                        if ski4 + ski3 + ski2 + ski1 <=P
                            M_B = horzcat(snow1,snow2,snow3,snow4);
                            Mat(snow4,:) = NaN;
                %           Mat
                            [ski5,snow5]=min(Mat(:,j));
                         else
                             break
                        end
        M_B(j,matrows)=[M_B]
        end
       Mat
end
     Mat 
     M_B

试图M_B从每次迭代中保存到矩阵,但我总是得到最后一个值M_B。目的是根据列 (j) 和行 (matrows) 获得一个 M_B 矩阵或单元格(也尝试过但没有成功),如果使用 Mat 的前 4 列,则为 pe:

1 2 3   4 %j column
8 0 0 [5,4] %number of M_B row

等等。
我做错了什么?

在那之后,如果可能的话,请帮助我缩小/压缩代码。

先感谢您。

4

1 回答 1

0

我不明白你的代码,但我看到你在每个循环中使用相同的变量,我认为这样的事情可以防止你覆盖它:

totalM_B(j,matrows)=[M_B]

您可能需要对其进行一些调整以使其正常工作,但我希望您能看到当前代码的问题。

于 2013-02-05T16:30:19.363 回答