user3636220
user3636220

Reputation: 1

finding all factors of a number

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

Upvotes: -2

Views: 2589

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112679

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

Upvotes: 2

Related Questions