Creating unknown-dimension array while executing

1 visualizzazione (ultimi 30 giorni)
Carlos
Carlos il 15 Apr 2016
Risposto: J. Webster il 15 Apr 2016
Hello everyone, I am trying to program decision-making in a maze. In this system there are N1 steps where you have to decide between n different paths. The number of paths per step doesn't have to be the same for each step.
Well, the thing is that I want the program to be able to work whatever number of steps are predefined at the beginning, and I want to store the probabilities of taking different decisions at different steps in a vector of the kind: u(1,3,1,2...) where the column would correspond to step 1,2,.. and the number in the column to the path chosen in each particular step.
I don't know if that's possible, and there are probably other ways of doing it, but I think this one is the most convenient for this particular problem.
Thanks a lot

Risposte (1)

J. Webster
J. Webster il 15 Apr 2016
As you probably know, to create an array where you know the number of elements, you can use
X = zeros(1,N);
That's preferred, but if you don't have any way of knowing how big the array will be, you can start off with an empty array and then grow it...
X = [];
while somecondition
newX = something;
X = [X newX]; %#ok<AGROW>
end
The %#ok<AGROW> is to keep Matlab from complaining about growing an array inside a loop.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by