Creating unknown-dimension array while executing

4 ビュー (過去 30 日間)
Carlos
Carlos 2016 年 4 月 15 日
回答済み: J. Webster 2016 年 4 月 15 日
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.

Community Treasure Hunt

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

Start Hunting!

Translated by