MATLAB: How to save an entire matrix

matrix

Hi! I am solving matrixs and getting an X matrix for each loop. How can I save in an matrix every value of X that I get after each loop? Because after I need to see which value is the lowest. Thanks!
Q=[0.005 0.01 0.02 0.03]';
S=[0.45 1.1 3.3 6.7]';
b=0.1;
c=1/b;
for n=2.1:b:5
%Matriz con Q y S
M=[Q(1)^2+Q(2)^2+Q(3)^2+Q(4)^2, Q(1)^(n+1)+Q(2)^(n+1)+Q(3)^(n+1)+Q(4)^(n+1);
n*(Q(1)^n+Q(2)^n+Q(3)^n+Q(4)^n),n*(Q(1)^(2*n-1)+Q(2)^(2*n-1)+Q(3)^(2*n-1)+Q(4)^(2*n-1))];
%Matriz de la igualdad
N=[S(1)*Q(1)+S(2)*Q(2)+S(3)*Q(3)+S(4)*Q(4);
n*(S(1)*Q(1)^(n-1)+S(2)*Q(2)^(n-1)+S(3)*Q(3)^(n-1)+S(4)*Q(4)^(n-1))];
%Solucion de la matriz es:
X=M\N
end

Best Answer

At each iteration, you can save the result of M\N to a matrix (if all these results are of the same dimension 2x1) or to a cell array:
% save results to cell array
solutions = {};
atSolution = 1;
for n=2.1:b:5
% do stuff
% save result
solutions{atSolution} = M\N;
atSolution = atSolution + 1;
end
You can then iterate over the solutions to find the lowest.
An alternative is to just keep track of the lowest after each iteration (using whatever criteria you have to determine if one solution is lower than the previously found lowest solution).