Shifting number to end of an array
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Star Strider
2016-8-31
See if changing your if test to:
if v(ii)==a
helps.
4 个评论
Guillaume
2016-8-31
编辑:Guillaume
2016-8-31
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
2020-9-28
@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
更多回答(2 个)
ledinh lam
2016-11-30
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 !
0 个评论
John BG
2016-8-31
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 个评论
Guillaume
2016-9-12
编辑:Guillaume
2016-9-12
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!