How to solve complex looping using Matlab

1 回表示 (過去 30 日間)
Astha
Astha 2013 年 3 月 1 日
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
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
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.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by