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 arrayfor p=1:10 R(p,1)=[p.*A(:,1) A(:,2) A(:,3)]; Rend At the end of Simulation, R will have all the elements you need. Related SolutionsMATLAB: How to store loop value in array You need to use an array to store all the values. If values are scalar or vectors of constant length, then you can use simple arrays. If, in each iteration, the result is of variable length, then you will need to use cell arrays. For exampleA = zeros(1, 10)for i = 2:10 A(i) = A(i-1) + 1;endSee the first example here: https://www.mathworks.com/help/matlab/ref/for.html MATLAB: How to change sign in a cell respPreStore = cellfun(@uminus,respPreStore,'uni',false); Related QuestionHow to store equation values in array or matrixVectorization of product of flipped vectorsSimple question in matlabHi, please if I have too matrix like this how can I calculate the distance between them and thank you
Best Answer