-1

I would like to create a matrix to hold the result of functions in a nested loop as follow:

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

Because list contains real numbers the indexing res(i,j) doesn't work. Cn anyone give me an idea on how to proceed?

Thanks in advance.

KH_
  • 305
  • 4
  • 13
  • Use loops like `for i=1:numel(list)` , `for j=1:numel(list)` and then where you're using the values of `list` in that function, use `list(i)` instead of `i` and `list(j)` instead of `j`. Also it would be better to use some other variable names since `i` and `j` represents `imaginary` numbers in MATLAB – Sardar Usama Aug 27 '17 at 19:33
  • Thanks Sardar, that actually solves my problem. – KH_ Aug 27 '17 at 19:47

1 Answers1

-1

All the suggested comments (working with indexes in a nested for) are valid for this answer, I will also recommend use something like this, the operation that you need is something simimlar to apply to a function to de cartesian product of the list so you can work as follow:

>> [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
anquegi
  • 11,125
  • 4
  • 51
  • 67