How to output several calculations using m file in the form of matrices?

I am writing an m file where iterations of the responses are computed. I want to save each of these iterations in a matrix. How should I do it?

    j = 0;

for j < n;  %n is a user input
    futurevalue = P*(1+i)^j;  % each of these calculation I want to save
    j = j+1;
end
+3
source share
1 answer

You define an array of cells and store the desired variable in it.

intermResults = cell(1,n);
for j = 1:n;  %n is a user input
    intermResults{j} = P*(1+i)^j;  % each of these calculation I want to save
end

Then you can access the xx value:

desiredIntermResult = intermResults{xx}

Btw. I did not know that MATLAB supports the ++ operator.

This is not true. I modified the code to match Matlab syntax - Jonas

+3
source

Source: https://habr.com/ru/post/1788100/


All Articles