MATLAB: Matrix from results of nested for / if loop

for loopif statementMATLABmatrix manipulation

Hi Community! I am trying to create a matrix from an equation, where one side of the equation is a set value (e.g. 31.5) and the other side is a product of two fractions (see code):
z1low = 17;
%Starting value for z1
z1high = 51;
%Final value for z1
z2low = 17;
%Starting value for z2
z2high = 306;
%Final value for z2
z3low = 17;
%Starting value for z3
z3high = 51;
%Final value for z3
z4low = 17;
%Starting value for z4
z4high = 306;
%Final value for z4
u = 31.5;
%Desired total gearing
for z1 = z1low: 1: z1high
for z2 = z2low: 1: z2high
for z3 = z3low: 1: z3high
for z4 = z4low: 1: z4high
if (z2/z1)*(z4/z3) == u
A = {z1, z2, z3, z4};
B = cell2mat(A(1:4));
disp(B)
disp(size(B))
end
end
end
end
end
The above code runs nicely through the for-loops and the if-condition, but it only returns the latest results for the z-values (thus the (1, 4) size of B).
Is there a simple way in MatLab to add each resulting row of the for/if-loop iteration to the bottom of a matrix? Thank you very much!

Best Answer

Replace
A = {z1, z2, z3, z4};
B = cell2mat(A(1:4));
with
B(end+1,:) = [z1, z2, z3, z4];