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

1 次查看(过去 30 天)
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
CS Researcher 2016-5-3
Use a cell array.
d1{1,h} = find(C1(:,:,h));
  3 个评论
CS Researcher
CS Researcher 2016-5-3
编辑:CS Researcher 2016-5-3
Do this:
[I(a,h),J(a,h)] = ind2sub(29,d1{1,h});
Hope this helps! Also, accept the answer if it worked for you so it can help others too.
AAQIB PEERZADA
AAQIB PEERZADA 2016-5-3
Before your reply I actually used this
[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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by