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?

조회 수: 2 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2016년 10월 15일
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
Josh
Josh 2016년 10월 15일
I would like to perform algebraic matrix multiplication of each 2x2 matrix.
The cat function does not change the cells to matrices therefore I can not use the prod function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by