필터 지우기
필터 지우기

Can someone help me understand this code

조회 수: 1 (최근 30일)
mat geek
mat geek 2019년 3월 31일
댓글: dpb 2019년 3월 31일
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
dpb
dpb 2019년 3월 31일
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');

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2019년 3월 31일
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.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by