-2

I am new to Matlab and for the values of a, l and w i need to find all the values for l in the data set and the corresponding w values.

a=10;
l=(0:10)
w=(0:10)
for l,d
       if a == l.*w
           disp(l) 
           disp(w)
       end
end
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147

1 Answers1

2

Not sure what you want to do, but I think your code could be put as follows:

a = 10;
l = 0:a; %// actually, it would suffice to check numbers up to floor(a/2)
ind = rem(a,l)==0; %// logical index that tells if remainder is zero or not
disp([l(ind); a./l(ind)])

Result:

     1     2     5    10
    10     5     2     1

You could do it more directly with Matlab's factor function:

f = factor(a);
disp([f; a./f])

Result:

     2     5
     5     2
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 1
    The `factor` function seems to be the way to go. If there is only interest in a restricted set of values, the output can afterwards be reduced. Check `help union`. – Dennis Jaheruddin May 14 '14 at 11:40