Shifting number to end of an array
Show older comments
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
More Answers (2)
ledinh lam
on 30 Nov 2016
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 !
John BG
on 31 Aug 2016
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
Star Strider
on 31 Aug 2016
Here ‘array’ means ‘vector’, as is obvious from the single index in her original code.
Guillaume
on 31 Aug 2016
@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.
John BG
on 11 Sep 2016
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.
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!