Effacer les filtres
Effacer les filtres

How to save variables and put them in a vector

46 vues (au cours des 30 derniers jours)
Feliciano Döring
Feliciano Döring le 29 Août 2018
Commenté : Walter Roberson le 29 Août 2018
I have this code in which i input a value, let's call it x. The code runs, and afterwards it stores a value called y. For each different x there's another y. What i want to do is each time i input an x it stores its value on a vector and the value from y on another vector so i can later plot them together.

Réponse acceptée

Walter Roberson
Walter Roberson le 29 Août 2018
Example:
num_runs = 10;
all_x = zeros(num_runs, 1);
all_y = zeros(num_runs, 1);
for K = 1 : num_runs
x = randi([-K K], 1, 1);
y = x.^2;
all_x(K) = x;
all_y(K) = y;
end
  4 commentaires
Feliciano Döring
Feliciano Döring le 29 Août 2018
In this case all_y and all_x are storing the variables somewhere or do i need to put my variables as cell arrays o i can store them properly?
Walter Roberson
Walter Roberson le 29 Août 2018
Script version:
global all_x all_y
x = input('Enter an x value ');
r = rand() * sqrt(x);
y = atan2(r, x);
all_x(end+1) = x;
all_y(end+1) = y;
Or better for the script case:
if ~exist('all_x', 'var'); all_x = []; all_y = []; end
x = input('Enter an x value ');
r = rand() * sqrt(x);
y = atan2(r, x);
all_x(end+1) = x;
all_y(end+1) = y;
all_x and all_y are storing all runs of the values. You would only need cell arrays if your values are not scalar numeric values.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by