Indexing & Move Me function
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
K Dani
il 20 Lug 2017
Commentato: Walter Roberson
il 8 Feb 2019
function out = move_me(v,a)
lng = length(v);
for i = 1:lng;
%evaluating each position of v in relation to a
if v(i) == a;
%if the value is equal, I deleted the value to then move it to the
%back of the function
v(i) = [];
v(lng) = a;
elseif v ~= a
v = [v, zeros(1, i)];
end
end
out = v
end
"The function move_me is defined like this: function w = move_me(v,a). The first input argument v is a row-vector, while a is a scalar. The function moves every element of v that is equal to a to the end of the vector. For example, the command
>> x = move_me([1 2 3 4],2);
makes x equal to [1 3 4 2]. If a is omitted, the function moves occurrences of zeros."
When I run the code, if the vector contains more than one of the same value of a it only moves one of the values to the back. Also, when trying to add zeros to the back if the vector does not contain a, more zeros are added than needed. I'm not quite sure why it is not working. Should I make the elseif statement its own if statement?
0 Commenti
Risposta accettata
Walter Roberson
il 20 Lug 2017
When you do the
v(i) = [];
then all of the items after index i "fall down" to occupy the missing space, so what was previously i+1 becomes located at i, what was at i+2 becomes located at i+1, and so on. You then fill in the empty slot at the end with an occurrence of "a", so the array maintains its length. However, when you increment "i", you fail to look at the value that "fell down" into the location "i".
You also have
elseif v ~= a
v = [v, zeros(1, i)];
end
This is testing the entire vector v for equality to a; the v ~= a part computes a logical vector. "elseif" considers the test to be true only if all of the members of the logical vector are true, equivalent to if you had tested
elseif all(v ~= a)
In the case where all of the v are different from a, you proceed to add a bunch of zeros on to the end of the existing v. The next loop through, if a is equal to 0, the test would fail because there would be some place for which v ~= 0 would fail, but if a was non-zero, then you would proceed to add even more zeros on to the end of v...
The instructions you were given about zeros are saying that you should test
if ~exist('a', 'var')
a = 0;
end
and otherwise do not do anything special about 0.
0 Commenti
Più risposte (2)
Anupriya Krishnamoorthy
il 17 Feb 2018
Modificato: Anupriya Krishnamoorthy
il 17 Feb 2018
% code
function w = move_me(v,a)
if nargin < 2 % checks whether function input is less than 2
a = 0;
w = [v(v~=a) v(v==a)];
else
w = [v(v~=a) v(v==a)];
end
end
0 Commenti
RAMAKANT SHAKYA
il 7 Feb 2019
function out= move_me(v,a)
s=length(v);
if nargin < 2 %for validation of input
a=0;
end
for x=1:s
if v(x)==a %comparing array element to the given no
t=x;
for y=t:s-1
v(y)=v(y+1); %shifting to right
end
v(s)=a;
end
out=v;
if v(x)==a
t=x;
for y=t:s-1
v(y)=v(y+1);%shifting to right
end
v(s)=a;
out=v;
end
if v(x)==a
t=x;
for y=t:s-1
v(y)=v(y+1);%shifting to right
end
v(s)=a;
out=v;
end
end
end
1 Commento
Walter Roberson
il 8 Feb 2019
Seems like a waste for something that can be implemented in one line by using logical indexing.
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!