Effacer les filtres
Effacer les filtres

Can someone help me understand this code

1 vue (au cours des 30 derniers jours)
mat geek
mat geek le 31 Mar 2019
Commenté : dpb le 31 Mar 2019
Can someone explain to me this solution to the code. I got most of the parts but there is a part where I don't understand why is it in that syntax but I couldn't find another way to get the same results. The goal in short, is to get rid of numbers in pinCode with consecutive repetitions. ex. 5, 5. and to display the positions of the consecutive repetitions numbers in empty array. I have a problem in the else statement where
PinCodeFix = [PinCodeFix PinCode(i)].
I wish someone can explain the syntax of this statement. Also, You'll see my own solution when tried the same result but I could't.
pinCode = [2,9,9,5,5,3];
repPos = [];
pinCodeFix = [pinCode(1)];
Vsize = length(pinCode);
x = 1;
for i = 2:1:Vsize
if pinCode(i) == pinCodeFix(x)
repPos = [repPos i]; %There is the part if the number in PinCode = PinCodeFix.
% The position will be stored in array. And I Understand this part.
else
pinCodeFix = [pinCodeFix pinCode(i)]; %% Here is the part when PinCode ~= PinCodeFix,
% it stores that number in PinCodeFix
%% Here is what I did when I tried to solve the code
% PinCodeFix = [pinCode(i)];
x = x +1;
end
end
  1 commentaire
dpb
dpb le 31 Mar 2019
As written, the else clause simply adds the next different element of the original array to the end of the output array. The end result is identical to just
pinCodeFix=unique(PinCode,'stable');

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 31 Mar 2019
A = [A value]
is equivalent to
A = horzcat(A, value)
which is equivalent to
temporary_variable = horzcat(A, value);
A = temporary_variable;
which is to say that you take the existing contents of A, and append the value to it, producing a new longer array, that you then assign over top of A. So you "grow" A by tacking the value on to the end of it.
Equivalent to this would be
A(end+1) = value;
which says to grow A by one location at the end and write the value into that location.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by