Creating unknown-dimension array while executing

1 次查看(过去 30 天)
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

回答(1 个)

J. Webster
J. Webster 2016-4-15
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by