how to save some variables created in a loop

2 vues (au cours des 30 derniers jours)
flashpode
flashpode le 10 Jan 2022
Modifié(e) : Jon le 10 Jan 2022
Hi so I got a loop that every time it goes creates some variables.Ex:
When n = 1 creates this variables ''x'', ''y'' and ''z'', for n = 2 creates those variables ''x'', ''y'' and ''z'' (the same ones). What I want is to save all the values created of ''x'' in the loop of n numbers in a variable named ''All_x_values'' and the same for ''y'' and ''z''. The problem I am getting is that is only being saved the last value

Réponses (1)

Jon
Jon le 10 Jan 2022
Modifié(e) : Jon le 10 Jan 2022
You need to provide a vector to collect your values in, so something like this
numIterations = 10; % put total iterations here
All_x_values = zeros(numIterations,1); % preallocate array to hold x values
for k = 1:numIterations
% do your calcs
% ...
%
% assign the resulting x value into vector holding results
All_x_values(k) = x
end
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by