How to move an element from an array?
Ältere Kommentare anzeigen
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 Kommentare
Guillaume
am 5 Sep. 2017
Can you provide an example of before and after?
GEORGIOS KOULIDIS
am 5 Sep. 2017
Bearbeitet: Stephen23
am 6 Sep. 2017
Pal Szabo
am 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
Akzeptierte Antwort
Weitere Antworten (1)
Andrei Bobrov
am 5 Sep. 2017
A(2,3:end) = circshift(A(2,3:end),1)
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!