Borrar filtros
Borrar filtros

Shifting number to end of an array

12 visualizaciones (últimos 30 días)
Katelin Cherry
Katelin Cherry el 31 de Ag. de 2016
Comentada: Sim el 28 de Sept. de 2020
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

Respuesta aceptada

Star Strider
Star Strider el 31 de Ag. de 2016
See if changing your if test to:
if v(ii)==a
helps.
  4 comentarios
Guillaume
Guillaume el 31 de Ag. de 2016
Editada: Guillaume el 31 de Ag. de 2016
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)];
Sim
Sim el 28 de Sept. de 2020
@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

Iniciar sesión para comentar.

Más respuestas (2)

ledinh lam
ledinh lam el 30 de Nov. de 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
John BG el 31 de Ag. de 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 comentarios
John BG
John BG el 11 de Sept. de 2016
and what do you think that circshift does?
Guillaume
Guillaume el 12 de Sept. de 2016
Editada: Guillaume el 12 de Sept. de 2016
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by