How to solve complex looping using Matlab

2 views (last 30 days)
Astha
Astha on 1 Mar 2013
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
  1 Comment
Jan
Jan on 1 Mar 2013
Please use meaningful tags, because they are used to classify the questions. All questions concern "Matlab" and 99% concern "Matlab code".

Sign in to comment.

Answers (1)

Jan
Jan on 1 Mar 2013
Edited: Jan on 1 Mar 2013
The Matrices A, B, C, ... contain very redundant elements. Are you sure that it is helpful to store 5 identical columns in a matrix?
If you avoid to store the data in different variables, but write them to one 3D-array, the operation gets very easy:
Array = cat(3, [A1, A1; A2, A2; A3, A3], [B1, B1; B2, B2; B3, B3]);
Result = permute(Array, [3,2,1]);
(here with 3 elements to save space...) Now the i.th matrix is not stores in the i.th variable, but in Result(:, :, i).
I assume the construction of your matrices do not need loops at all:
A = repmat([A1, A2, A3].', 1, 3);
Or following the above advice, you can write them to a 3D-array directly.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!