how to access cell array data with single for loop

two cell array like
A={1,2,3,4,5} %cell array
B={11,12,13,14,15} %cell array
for i=1:length(A)
C %variable
D %variable
end
in first iteration C take 1 from A and D take 11 from B
second iteration C take 2 from A and D take 12 from B
So on
use only single for loop

2 件のコメント

Stephen23
Stephen23 2015 年 4 月 27 日
編集済み: Stephen23 2015 年 4 月 27 日
Why are these values in cell arrays anyway? Unless you have mixed data types or different sizes, just use a simple numeric array to store those values.
Also note that, depending on what operations you need to perform, the entire loop might be able to be replaced with vectorized code. If you show us what the operations are inside the loop, then we could show you if this can be simplified by vectorization.
singh
singh 2015 年 4 月 27 日
i have some problem because actual cell array value is below
A{:}
17
6 11
1 9
B{:}
1 9
6 11
17

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

 採用された回答

per isakson
per isakson 2015 年 4 月 27 日
編集済み: per isakson 2015 年 4 月 27 日

2 投票

C = cell( size(A));
D = cell( size(B));
for ii=1:length(A)
C(ii) = A(ii);
D(ii) = B(ii);
end
&nbsp
Addendum
Without the loop - vectorized
C = A;
D = B;

4 件のコメント

singh
singh 2015 年 4 月 27 日
C have that assign value until that iteration will be expire
when expire the iteration C does not contain that value then C contain next only next value
suppose C contain first value from A in iteration 1 but in iteration 2 C contain only second value and then third value in third iteration and so on
DEEPAK PHCSFI17041149
DEEPAK PHCSFI17041149 2017 年 11 月 27 日
Hey Isak, any idea how to do it for saving Variable C as C1, C2, C3 on loops? i mean when A = {1,2,3,4,5} Then C1 = 1, C2 = 2, C3 = 3, C4 = 4, C5= 5?
DEEPAK PHCSFI17041149
DEEPAK PHCSFI17041149 2017 年 11 月 29 日
Thanks for the guidance Isak.

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

その他の回答 (1 件)

Michael Haderlein
Michael Haderlein 2015 年 4 月 27 日

0 投票

You mean like
for cnt=1:length(A)
C=A{cnt};
D=B{cnt};
end
Actually, there is no need for cell arrays here. In case this is just a simplified example and A and B will be of different kind in your final program, it's fine. Otherwise, I would make A and B just normal double arrays.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by