MATLAB: How to store results from loops and put them in matrice

MATLABstoring in matrice

I have this propostion:
clear all, clc
X=[0 1], Y=[0 1], Z=[0 1];
A=allcomb(X, Y,Z);
for p=1:10
R=[p.*A(:,1) A(:,2) A(:,3)];
R
end
I want that results of each iteration will be stored in matrice

Best Answer

  • Just pre define R as an empty array and use it inside the loop with suitable indexing. Something like this:
    R=zeros(10,1); % Pre define R as zero array
    for p=1:10
    R(p,1)=[p.*A(:,1) A(:,2) A(:,3)];
    R
    end
    At the end of Simulation, R will have all the elements you need.