Creating unknown-dimension array while executing

조회 수: 1 (최근 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.

카테고리

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