How can I use "for loop" for this script?
古いコメントを表示
Hi, I have a random matrix with large dimension and I have to form augmented matrix. My script for given dimensions is good but for random and large matrices I need a "for loop". For example:
X = rand(3,5);
[n,m]=size(X);
A0=X(:,m);
A1=[X(:,m-1); A0];
A2=[X(:,m-2); A1];
A3=[X(:,m-3); A2];
A4=[X(:,m-4); A3];
Now, A4 gives the first column of augmented matrix. The other columns can be calculated in the same way. But I need a loop to calculate all the columns regardless of the dimension of X. Assume that X is 3*100. How can I calculate the augmented matrix?
採用された回答
その他の回答 (1 件)
mili ss
2015 年 12 月 8 日
0 投票
1 件のコメント
Mohammad Abouali
2015 年 12 月 8 日
編集済み: Mohammad Abouali
2015 年 12 月 8 日
The definition of XD seems to be different than what you explained.
X(:) gives only the step 1, which was based on your code.
Based on the XD definition in 02.jpg here how you can do it:
XD=[ reshape(X(:,1:end-2),[],1), ...
reshape(X(:,2:end-1),[],1), ...
reshape(X(:,3:end),[],1)]
Here is an example:
X=reshape(1:15,3,5)
X =
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15
XD=[ reshape(X(:,1:end-2),[],1), ...
reshape(X(:,2:end-1),[],1), ...
reshape(X(:,3:end),[],1)]
XD =
1 4 7
2 5 8
3 6 9
4 7 10
5 8 11
6 9 12
7 10 13
8 11 14
9 12 15
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!