How to move an element from an array?
Afficher commentaires plus anciens
I have an array and i want to to move the element a which located in x line, by one position to the right without change the left part
3 commentaires
Guillaume
le 5 Sep 2017
Can you provide an example of before and after?
GEORGIOS KOULIDIS
le 5 Sep 2017
Modifié(e) : Stephen23
le 6 Sep 2017
Pal Szabo
le 6 Sep 2017
If you have this as original array:
A=[ 1 2 3 4 0 0 0;
5 3 2 1 0 0 0;
4 4 4 4 0 0 0;
2 3 4 5 6 7 8]
You want to move the elements starting from line row=2, column=3 (this is your number two), and replace the element originally in position row=2 column=3 by another element stored in the variable replacewith (in this case 0), then write:
A=[ 1 2 3 4 0 0 0;
5 3 2 1 0 0 0;
4 4 4 4 0 0 0;
2 3 4 5 6 7 8]
x=2
y=3
replacewith=0;
result_xth_row=[A(x,1:y-1),replacewith,A(x,y:end-1)]
A(x,1:end)=result_xth_row
A
Réponse acceptée
Plus de réponses (1)
Andrei Bobrov
le 5 Sep 2017
A(2,3:end) = circshift(A(2,3:end),1)
Catégories
En savoir plus sur Creating and Concatenating Matrices 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!