I declared a 3-dimension matrix like this , and later in the iteration, I need to dynamically expand the length of the third dimension.
Notice that currently, I only declare two dimensions but not the third dimension, so I will get an error like this:
Attempted to access bins(1,2,2); index out of bounds because
size(bins)=[2,22,1].
But the problem is, the Xk dimension is variable and I really cannot decide how it is needed. Is there anyway to declare a changeable matrix?
bins=zeros(2,size(Xtrain,2));
%Some for loop
bins(Y,k,Xk)=bins(Y,k,Xk)+1;
%end loop
You can use the
end + knotation to dynamically expand the matrix as follows:The size of
Awill have increased automatically and Matlab will automatically fill in-between values with zeros. However, resizing an array inside a loop is not a good way to program. You might want to rethink your method such that you aren’t doing this.