matrix step shift in each row

10 vues (au cours des 30 derniers jours)
mohammed hussein
mohammed hussein le 23 Fév 2021
Commenté : mohammed hussein le 23 Fév 2021
HI
i have equestion about matrix shift numbers . for example i have this matrix
A =[ 1 2 3 4 5 ]
i want it to be
A =
1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
thank you very much for helping
  2 commentaires
Rik
Rik le 23 Fév 2021
Do the numbers in A mean anything (e.g. the number of positions the corresponding row should be shifted), or are you simply asking about a row-wise circshift?
mohammed hussein
mohammed hussein le 23 Fév 2021
yes , i have circle geometry , i want to shift the number in circle like the simple example that i asking about

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 23 Fév 2021
This method works without a loop.
A =[ 1 2 3 4 5 ];
[X,Y]=ndgrid(1:numel(A));
X=X(end:-1:1,:);
ind=mod(X+Y,numel(A));
ind(ind==0)=numel(A);
A(ind)
ans = 5×5
1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1
  1 commentaire
mohammed hussein
mohammed hussein le 23 Fév 2021
thank you very much . this exactly what i want

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 23 Fév 2021
A = [1,2,3,4,5];
B = toeplitz(A([1,end:-1:2]),A)
B = 5×5
1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1
  1 commentaire
mohammed hussein
mohammed hussein le 23 Fév 2021
Thank you very much

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by