How can I define a variable with separate variables for later use?

1 vue (au cours des 30 derniers jours)
Malcolm Sturgis
Malcolm Sturgis le 9 Avr 2020
% simply an example
car = [a b c]
a = 2
car(1)
I want this to return 2 but it cannot even define car because a b and c are undefined. Is there a way to define car using variables like a b and c which can be defined later?
% an attempted fix that did not work
a = 1; b = 1; c = 1;
car = [a b c]
a = 2;
car(1)
this returns 1 because car was defined from the a which was 1 but I want a to be dynamic inside of car such that this would return 2. Help please!
  2 commentaires
Optics Wizard
Optics Wizard le 9 Avr 2020
Not really, but this might help:
You can assign value to car(1), for instance:
car(1) = 2;
So if you want to define 100 empty variables for later use, you can just write:
variables = zeros(1,100);
Then when you want to populate all the variables:
for n = 1:100
variables(n) = rand();
end
Is that sufficient?
Malcolm Sturgis
Malcolm Sturgis le 9 Avr 2020
Not exactly what I was looking for but I think this will help me rework my code in a way I don't need my original thingy.
Thanks for the quick response!

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Elementary Math 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