Question

Let there be five matrices given as:

 A=  [A1 A1 A1 A1 A1; A2 A2 A2 A2 A2; A3 A3 A3 A3 A3]

 B= [B1 B1 B1  B1  B1; B2 B2  B2 B2 B2;B3 B3 B3 B3 B3]

 C=[ C1 C1 C1 C1 C1; C2 C2 C2 C2 C2; C3 C3 C3 C3 C3]

 D= [D1 D1 D1 D1 D1 ; D2 D2 D2 D2 D2; D3 D3 D3 D3 D3]

 E=[ E1 E1 E1 E1 E1; E2 E2 E2 E2 E2; E3 E3 E3 E3 E3]

I want to make a program such that ouput consists of taking each row of each given matrix and forming a new matrix. how to use looping in such cases when length of matrices increases and number of given matrices also increases. This problem seemed to me a complex one. Because I want to generalize by using loop and output for any number of matrices say 20 and having number of columns also increased to say 25, then how to get these P1 to P20 outputs. Can anyone help me regarding this complex trouble using Matlab

P1=[ A1 A1  A1 A1 A1; B1 B1 B1 B1 B1; C1 C1 C1 C1 C1 C1; D1 D1 D1 D1 D1; E1 E1 E1 E1 E1]
P2=[ A2 A2  A2 A2 A2; B2 B2 B2 B2 B2; C2 C2 C2 C2 C2 C2; D2 D2 D2 D2 D2; E2 E2 E2 E2 E2]

and similarly other matrices is obtained .

Note: That the given 5 matrices are generated with help of loop. So first I would be getting values as :

  A= A1
  B= B1
  C=C1
  D=D1
  E=E1

  A= A1 A1 
  B= B1 B1
  C=C1  C1
  D=D1  D1
  E=E1  E1 .... AND SO ON
Était-ce utile?

La solution

Get a loop and put all the matrix together to form a 3D tensor. Or just put the matrices in the 3D tensor when they are created.

M(:,:,1) = A; M(:,:,2) = B; etc

then

squeeze(M(1,:,:))' is the P1, squeeze(M(2,:,:))' is the P2

Example:

M(:,:,1) =

     1     2
     3     4


M(:,:,2) =

     5     6
     7     8

>> squeeze(M(1,:,:))'

ans =

     1     2
     5     6
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top