保存(在矩阵中)大量情况的经过时间和迭代次数

时间:2015-07-18 21:05:37

标签: matlab matrix time

如果输入 A1,A2,A3,A4 ,我有一个输出迭代次数测试值的程序。< / p>

我希望运行A1,A2,A3,A4各5个值,从而进行625次运行。在此过程中,我想在3个单独的矩阵中保存每次运行的已用时间迭代次数测试值。< / p>

我尝试使用4个嵌套用于循环,并取得了进展,但需要一些帮助来索引矩阵的元素。 for循环中的迭代器变量与索引变量不匹配...

4个嵌套循环的代码如下:

m = logspace(-4,4,5);
n = logspace(0,8,5);
eltime = zeros(5,length(m)*length(m)*length(m));

for A1 = m
    for A2 = m
        for A3 = m
            for A4 = n
                tic
                SmallMAX(A1,A2,A3,A4)
                toc;
                for i=1:numel(eltime)
                    for j = 1:length(n)
                        eltime(j,i) = toc;
                    end
                end
            end
        end
    end
end

主程序的代码摘录如下:

function [k,test] = SmallMAX(A1,A2,A3,A4)
...
end

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在您的情况下,最简单的方法是使用A1A2A3A4作为计数器而不是实际值。这样他们就可以为eltime的条目编制索引。然后,我们可以使用sub2ind轻松计算第二维中的索引,并使用A4来索引eltime的第一维。我们还需要调整SmallMAX中的参数。

以下是建议方法的代码:

m = logspace(-4,4,5);
n = logspace(0,8,5);
eltime = zeros(length(n),length(m)*length(m)*length(m));
res_k = zeros(length(n),length(m)*length(m)*length(m)); % or zeros(size(eltime));
res_test = zeros(length(n),length(m)*length(m)*length(m)); % or zeros(size(eltime));

for A1 = 1:length(m)
    for A2 = 1:length(m)
        for A3 = 1:length(m)
            for A4 = 1:length(n)
                ind = sub2ind([length(m),length(m),length(m)],A3,A2,A1);
                tic
                [k,test] = SmallMAX(m(A1),m(A2),m(A3),n(A4));
                eltime(A4,ind) = toc;
                res_k(A4,ind) = k;
                res_test(A4,ind) = test;
            end
        end
    end
end

这是eltime

的已解决条目的顺序
eltime_order =
  Columns 1 through 18
     1     6    11    16    21    26    31    36    41    46    51    56    61    66    71    76    81    86
     2     7    12    17    22    27    32    37    42    47    52    57    62    67    72    77    82    87
     3     8    13    18    23    28    33    38    43    48    53    58    63    68    73    78    83    88
     4     9    14    19    24    29    34    39    44    49    54    59    64    69    74    79    84    89
     5    10    15    20    25    30    35    40    45    50    55    60    65    70    75    80    85    90
  Columns 19 through 36
    91    96   101   106   111   116   121   126   131   136   141   146   151   156   161   166   171   176
    92    97   102   107   112   117   122   127   132   137   142   147   152   157   162   167   172   177
    93    98   103   108   113   118   123   128   133   138   143   148   153   158   163   168   173   178
    94    99   104   109   114   119   124   129   134   139   144   149   154   159   164   169   174   179
    95   100   105   110   115   120   125   130   135   140   145   150   155   160   165   170   175   180
  Columns 37 through 54
   181   186   191   196   201   206   211   216   221   226   231   236   241   246   251   256   261   266
   182   187   192   197   202   207   212   217   222   227   232   237   242   247   252   257   262   267
   183   188   193   198   203   208   213   218   223   228   233   238   243   248   253   258   263   268
   184   189   194   199   204   209   214   219   224   229   234   239   244   249   254   259   264   269
   185   190   195   200   205   210   215   220   225   230   235   240   245   250   255   260   265   270
  Columns 55 through 72
   271   276   281   286   291   296   301   306   311   316   321   326   331   336   341   346   351   356
   272   277   282   287   292   297   302   307   312   317   322   327   332   337   342   347   352   357
   273   278   283   288   293   298   303   308   313   318   323   328   333   338   343   348   353   358
   274   279   284   289   294   299   304   309   314   319   324   329   334   339   344   349   354   359
   275   280   285   290   295   300   305   310   315   320   325   330   335   340   345   350   355   360
  Columns 73 through 90
   361   366   371   376   381   386   391   396   401   406   411   416   421   426   431   436   441   446
   362   367   372   377   382   387   392   397   402   407   412   417   422   427   432   437   442   447
   363   368   373   378   383   388   393   398   403   408   413   418   423   428   433   438   443   448
   364   369   374   379   384   389   394   399   404   409   414   419   424   429   434   439   444   449
   365   370   375   380   385   390   395   400   405   410   415   420   425   430   435   440   445   450
  Columns 91 through 108
   451   456   461   466   471   476   481   486   491   496   501   506   511   516   521   526   531   536
   452   457   462   467   472   477   482   487   492   497   502   507   512   517   522   527   532   537
   453   458   463   468   473   478   483   488   493   498   503   508   513   518   523   528   533   538
   454   459   464   469   474   479   484   489   494   499   504   509   514   519   524   529   534   539
   455   460   465   470   475   480   485   490   495   500   505   510   515   520   525   530   535   540
  Columns 109 through 125
   541   546   551   556   561   566   571   576   581   586   591   596   601   606   611   616   621
   542   547   552   557   562   567   572   577   582   587   592   597   602   607   612   617   622
   543   548   553   558   563   568   573   578   583   588   593   598   603   608   613   618   623
   544   549   554   559   564   569   574   579   584   589   594   599   604   609   614   619   624
   545   550   555   560   565   570   575   580   585   590   595   600   605   610   615   620   625