# Hello everyone! I need some help regarding creating a dynamic/flexible array/matrix in matlab?

1 view (last 30 days)
AAQIB PEERZADA on 3 May 2016
Commented: AAQIB PEERZADA on 3 May 2016
Here is my question. I have matrix which is updating itself at every iteration, and at each iteration i have to extract some information from this matrix and store it in another matrix. The information coming from the first matrix keeps changing with every iteration and hence I have changing data coming in which I need to store in a different matrix. So the size of the other matrix will change at each iteration. MATLAB gives me an error, "subscript assignment dimension mismatch", because the dimension of other matrix changes. here is my code
for h=1:1:length(Lf)-5
d1(:,h)=find(C1(:,:,h));
for a=1:1:length(d1(:,h))
[I(a,h),J(a,h)]=ind2sub(29,d1(a,h));
end
for g=1:1:length(d1(:,h))
Clusters(g,:,h)=[I(g,h) J(g,h)];
end
So here C1 is a matrix i have already created which has the same dimensions every time. But i have to get the indices of the non-zero elements of C1 at each iteration(h), and store those linear indices in d1. And d1 changes size at every iteration(h), because the number of non-zero elements in C1 change with each iteration count. So when i try to execute
d1(:,h)=find(C1(:,:,h));
At the second iteration number (h=2), MATLAB says "subscript assignment dimension mismatch", because the number of non-zero elements at h=2 is different than at h=1, and they are different for all other values of h.
So is there a way I can create this dynamic array/matrix "d1", such that it can accommodate the linear indices of any number of non-zero elements coming from C1 at each iteration, without giving me any error?

CS Researcher on 3 May 2016
Use a cell array.
d1{1,h} = find(C1(:,:,h));
AAQIB PEERZADA on 3 May 2016
[I ,J]=ind2sub(29,d1{1,h});
This worked. What you suggested, I tried that, but errors kept popping up. Anyway your idea to use a cell array helped me a lot. I used the same idea for other problems as well. Thank you very much!