How can I store nested for loop values to a cell array?

I am using a nested for loop. Loop executed values are being stored in cell array but for the first row its working well but from the secong row the last cell value of first row is being repeated . how to solve this ?

1 件のコメント

Adam
Adam 2019 年 2 月 26 日
You haven't shown any code so it's rather difficult to guess!

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2019 年 2 月 26 日
編集済み: Adam Danz 2019 年 2 月 26 日

1 投票

Here's a general sketch of how to store data in a cell array within two nested for-loops. If there is something more specific you're looking for, please add some details.
myCell = cell(n,m);
for i = 1:n
for j = 1:m
myCell{i,j} = myFunction(data);
end
end

4 件のコメント

Adam Danz
Adam Danz 2019 年 2 月 26 日
編集済み: Adam Danz 2019 年 2 月 26 日
The code doesn't function without the png files and that makes it difficult to trouble shoot. But you have the png files so I can suggest where to troubleshoot.
The "profile" vector looks like it varying in length but you're never clearing that variable between iterations. In other words, imagine upon the first iteration
profile=[0 1 2 3 4 5 6]
and on the second iteration profile is supposed to equal
profile=[9 9 9]
but since you never cleared the variable, it actually equals
profile=[9 9 9 3 4 5 6]
My guess is that you're storing the values in "A" correctly but you're incorrectly creating the 'profile' variable. Here's how to correct that: (assuming 'profile' is a row vector)
profile = zeros(1, length(loo)); % <--- add this line here
for l=1:length( loo )
profile(l) = Y(yTop(l),xTop(l));
end
Zara Khan
Zara Khan 2019 年 2 月 27 日
Thank you so much, now its working well.
But one thing I want to ask you , is there any alternative of using cell array ? Beacuse this code taking 1 hour of time to execute .
Adam Danz
Adam Danz 2019 年 2 月 27 日
There are alternative methods of storing values within a loop but the cell-array you've got is already a good method and it consuming microseconds of time.
When possible, you should always initialize storage variables prior to the loops. Your ell array will always have 'numImages' number of rows but since I cannot run your code, I can't easily tell how many columns it will have. If you can know ahead of time the number of columns, allocate the 'A' array prior to the loops. That will save time.
A = cell(numImages, h)
If you want to understand what part of your code is cuasing the bottleneck, you can run matlab's profile() function which will produce a table of processing times for each component of your code.
profile on
myFunction() %<-- run your function / script
profile viewer
Zara Khan
Zara Khan 2019 年 3 月 2 日
Thank you .

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB Support Package for USB Webcams についてさらに検索

質問済み:

2019 年 2 月 26 日

コメント済み:

2019 年 3 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by