How to move an element from an array?
Show older comments
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 Comments
Guillaume
on 5 Sep 2017
Can you provide an example of before and after?
GEORGIOS KOULIDIS
on 5 Sep 2017
Edited: Stephen23
on 6 Sep 2017
Pal Szabo
on 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
Accepted Answer
More Answers (1)
Andrei Bobrov
on 5 Sep 2017
A(2,3:end) = circshift(A(2,3:end),1)
1 Comment
GEORGIOS KOULIDIS
on 5 Sep 2017
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!