How can I use "for loop" for this script?
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
mili ss
le 8 Déc 2015
0 votes
1 commentaire
Mohammad Abouali
le 8 Déc 2015
Modifié(e) : Mohammad Abouali
le 8 Déc 2015
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
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!