MATLAB: Storing the values used to calculate solution from a for loop

forloophomework

I need to make a code that will take a 1×19 vector of numbers (number of teeth on the gears available) then divide every number within the vector by one another to create a new 19×19 matrix, then multiply every number from that matrix by one another to create a new matrix. From this new matrix I must identify every solution that is over 16. I have figured all of that out. Except now I somehow have to figure out which combination of four original numbers got me a solution greater than 16. Here is what I have.How would I figure out which values from the gearteeth vector give me a solution greater than sixteen using when they are put through the following formula (a/b)*(c/d).
GearTeeth=[96,80,72,64,56,52,48,44,42,34,32,30,24,22,20,20,18,16,12];
n=length(GearTeeth);
for i=1:n
for j=1:n
TorqueRatio(i,j)=GearTeeth(i)/GearTeeth(j);
j=j+1;
end
end
m=1;
for k=1:361
for l=1:361
GearRatio(k,l)=TorqueRatio(k)*TorqueRatio(l);
if GearRatio(k,l)>16
A(m)=(GearRatio(k,l));
m=m+1;
end
l=l+1;
end
end

Best Answer

You have a neat formula: "(a/b)*(c/d)". So start at this and implement it as direct as possible:
GearTeeth = [96,80,72,64,56,52,48,44,42,34,32,30,24,22,20,20,18,16,12];
Result = zeros(length(GearTeeth) ^ 4, 4); % Pre-allocate
m = 0;
for a = GearTeeth
for b = GearTeeth
for c = GearTeeth
for d = GearTeeth
if (a/b)*(c/d) > 16
m = m + 1;
Result(m, :) = [a,b,c,d];
end
end
end
end
end
Result = Result(1:m, :); % Cut off unused results
The problem sounds like a homework question. So mention the source, if you post this solution. Teachers read this forum also.