How to solve complex looping using Matlab
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Jan
2013년 3월 1일
Please use meaningful tags, because they are used to classify the questions. All questions concern "Matlab" and 99% concern "Matlab code".
답변 (1개)
Jan
2013년 3월 1일
편집: Jan
2013년 3월 1일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!