MATLAB: How to return the values of a loop loopreturnvalue I want to return the entire values of k matrice but I only end up with values from the last row. for i = 1:5, j = 0:i; k = i.^2 + i*j + j.^2end Best Answer You are computing k once for each value of i and overwriting it. If I understand what you want, try: i = 1:5; j = 0:5; k = zeros(numel(i),numel(j)); for a = 1:numel(i) for b = 1:numel(j) k(a,b) = i(a)^2 + i(a)*j(b) + j(b)^2; end end display(k) Related SolutionsMATLAB: Pre allocating triple loops n = numel(L);c = cell(n,n,45);for ii = 1:n for jj = 1:n for kk = 1:45 c{ii,jj,kk} = cross(L1(ii,:),a{jj}(kk,:)); end end end MATLAB: How to create a matrix of coordinates Hi,Try something like this.n = input('Enter value')for i = 1:2 for j = 1:n for k = 1:n if i == 1 a(i,j,k) = n-j+1; elseif i == 2 a(i,j,k) = k end end endend Related QuestionHow to collect non-integers from for loop in one matrixConcatenate Aphabets and numbersStoring and passing all iterations to an array outside the nested for loopsHow to store values as arrayHow to update matrix values using algorithm based on position
Best Answer