Given two 1 x n cells namely, C1 and C2, how do I create a new cell C3 such that C3 = {C1(1) C2(1) C1(2) C2(2) C1(3) C2(3) ... } using a for loop?

1 view (last 30 days)
For example suppose you are given cells
C1 = {[a1 b1; c1 d1] [a2 b2; c2 d2] [a3 b3; c3 d3] [a4 b4; c4 d4]}; C2 = {[e1 f1; g1 h1] [e2 f2; g2 h2] [e3 f3; g3 h3] [e4 f4; g4 h4]};
(i.e. both cells C1 and C2 contain four 2x2 matrices)
How would I using a for loop create a new cell C3 such that
C3 = {C1{1,1} C2{1,1} C1{1,2} C2{1,2} C1{1,3} C2{1,3} C1{1,4} C2{1,4}}.
Afterwards, I plan to use the function cell2mat() and prod() in order to multiply all the matrices.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Oct 2016
for K = 1 : 1 %it's a loop
C3 = {C1{1,1} C2{1,1} C1{1,2} C2{1,2} C1{1,3} C2{1,3} C1{1,4} C2{1,4}};
end
Alternately, and less efficiently,
C3 = cell(1,8);
for K = 1 : 4
C3(2*K-1) = C1(1,K);
C3(2*K) = C2(1,K);
end
You can cell2mat(C3) but you are going to get out a plain numeric array, 2 x 16 . If you prod() that, you would get out a 1 x 16 result.
  4 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by