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!