Shifting number to end of an array

Hi, I'm trying to write a function that takes an array (v) and a value(a) as its input then outputs an array(w). The output array takes any instance of the value out of its current position and moves it to the end of the array. This is a hw problem for school. My function works perfectly when the value a is nonzero but it doesn't work for zero values. I have included my function below. I'm afraid it's something with the for loop and iterating through it. I'm pretty new to Matlab and can't figure this out. Thanks!
function v=move_me(v,a)
if nargin<2
a=0;
end
ii=0;
for ii=1:length(v)
if ii==a
v(ii)=[]
v(end+1)=a;
w=v;
end
end
if true
% code
end

 Accepted Answer

See if changing your if test to:
if v(ii)==a
helps.

4 Comments

Ahhhh yes. That worked!
I just realized that if there is more than one instance of "a" in the array, it only moves the first instance. I thought a for loop would go through each iteration and perform the if statement when true each time?
I tested it, and with that change, it will shift every value equal to ‘a’ to the end. I made one other small change to your code that allowed that to work:
for ii=1:length(v)
if v(ii)==a
v(end+1)=a;
v(ii)=[];
w=v;
end
end
Example:
v = randi([0 9], 1, 15); % Create Random Vector
q = v % Store Original Vector
a = 0;
for ii=1:length(v)
if v(ii)==a
v(end+1)=a;
v(ii)=[];
w=v;
end
end
v
q =
Columns 1 through 14
5 5 8 0 5 1 7 1 5 0 6 3 6 0
Column 15
1
v =
Columns 1 through 14
5 5 8 5 1 7 1 5 6 3 6 1 0 0
Column 15
0
All three zeros are at the end of the vector, as promised.
You cannot delete elements of a vector while iterating over the vector index. Your index will get out-of-sync.
The above code will fail for example on:
v = [1 5 5 1 5 5 1]
a = 5
You will either have to restart the loop from scratch every time you delete an element, or, better, store the indices of elements to move in the loop, and move them all at once after the loop.
And of course, the whole thing can be achieved without a loop:
v = [v(v ~= a), v(v == a)];
@Guillaume,
What if i want to move 2 or more elements to the end of the array?
v = [1 5 3 1 5 5 1]
a = [3 5];
I did it with a "for loop", but maybe there is a way without a loop...
for i = 1 : length(a)
v = [v(v ~= a(i)), v(v == a(i))]
end

Sign in to comment.

More Answers (2)

as they said above . you can use this code.
function v = move_me(v,a)
if nargin <2
a = 0;
end
v = [v(v ~= a), v(v == a)];
end
Maybe, it will help you !
When you say 'to the end' of the array, what happens to the values shifted beyond the size of the array, do they show up at the beginning of the array?
or do you pad zeros at the beginning, losing data?
In any case you may want to use circshift?
A=[1:10]
circshift(A,3,2)
=
Columns 1 through 5
8.00 9.00 10.00 1.00 2.00
Columns 6 through 10
3.00 4.00 5.00 6.00 7.00
A=[1:10]'
circshift(A,3)
=
8.00
9.00
10.00
1.00
2.00
3.00
4.00
5.00
6.00
7.00
or
circshift(A,[-1 -1])
=
1.00 0 0 1.00
0 0 0 0
0 0 0 0
1.00 0 0 1.00
circshift(A,[1 1])
=
0 0 0 0
0 1.00 1.00 0
0 1.00 1.00 0
0 0 0 0
may be you mean shifting linearly, in such case you may want to combine circshift with reshape
A=magic(4)
=
16.00 2.00 3.00 13.00
5.00 11.00 10.00 8.00
9.00 7.00 6.00 12.00
4.00 14.00 15.00 1.00
[sz1 sz2]=size(A)
B=reshape(A,[sz1*sz2,1]);
B2=circshift(B,3,1); % the shift factor is 3
C=reshape(B2,size(A))
=
8.00 5.00 11.00 10.00
12.00 9.00 7.00 6.00
1.00 4.00 14.00 15.00
16.00 2.00 3.00 13.00
Regarding the what dimension to shift along, or whether its circular shift, please clarify so a satisfactory answer can be supplied.
If you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

4 Comments

Here ‘array’ means ‘vector’, as is obvious from the single index in her original code.
@John, you completely missed the point.
As stated by Katelin, this is homework and they are being asked to develop an algorithm that simply does:
v = [v(v~=a), v(v==a)];
using a loop, with v being a vector. All they have to do is move elements equal to a to the end of the vector. Nothing more, nothing less.
and what do you think that circshift does?
I'll repeat (not that it matter anymore as this thread is out of date):
"They are being asked to develop an algorithm using a loop that does:"
v = [v(v~=a), v(v==a)];
What do you think circshift does? Certainly not the above. Given
v = [1 2 1 2 3 1 2 3 4]
a = 2
you should end up with
v = [1 1 3 1 3 4 2 2 2]
Your whole business about circshifting matrices misses the point, the problem is only concerned with vectors. Your question about what happens to values shifted beyond the size of the array is also irrelevant.

Sign in to comment.

Categories

Asked:

on 31 Aug 2016

Commented:

Sim
on 28 Sep 2020

Community Treasure Hunt

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

Start Hunting!