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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by