在matlab中嵌套循环内部索引

时间:2017-08-27 19:22:02

标签: matlab matrix mat

我想创建一个矩阵来保存嵌套循环中的函数结果,如下所示:

list = [0.01; 0.03; 0.1; 0.3; 1; 3; 10; 30];

res = zeros((size(list,1)),(size(list,1)));

for i = list
  for j = list
      res(i,j)=function(depending on i and j values from the list) goes 
               here); % This is the part where I need help
   end
end

因为列表包含实数,索引res(i,j)不起作用。有谁知道如何进行?

提前致谢。

1 个答案:

答案 0 :(得分:-1)

所有建议的注释(使用嵌套for中的索引)对这个答案都有效,我也建议使用这样的东西,你需要的操作是类似的,适用于de cartesian产品的函数列表,以便您可以按照以下方式工作:

>> [X,Y] = meshgrid(list,list);
>> abs(X - Y)

ans =

         0    0.0200    0.0900    0.2900    0.9900    2.9900    9.9900   29.9900
    0.0200         0    0.0700    0.2700    0.9700    2.9700    9.9700   29.9700
    0.0900    0.0700         0    0.2000    0.9000    2.9000    9.9000   29.9000
    0.2900    0.2700    0.2000         0    0.7000    2.7000    9.7000   29.7000
    0.9900    0.9700    0.9000    0.7000         0    2.0000    9.0000   29.0000
    2.9900    2.9700    2.9000    2.7000    2.0000         0    7.0000   27.0000
    9.9900    9.9700    9.9000    9.7000    9.0000    7.0000         0   20.0000
   29.9900   29.9700   29.9000   29.7000   29.0000   27.0000   20.0000         0