Hii I am not understanding this code please help and explain me step by step and please explain me idx2 = [0, idx, 0] what does it do.

1 view (last 30 days)
idx = EV > 0;
if iscolumn(idx), idx = idx'; end;
idx2 = [0, idx, 0];
prePt = []; postPt = [];
for i = 2 : length(idx2)-1
if idx2(i-1) == 0 & idx2(i) == 1 & idx2(i+1) == 1
prePt = [prePt, i-1];
end
if idx2(i-1) == 1 & idx2(i) == 1 & idx2(i+1) == 0
postPt = [postPt,i-1];
end
end
segNum = length(prePt);
segment = [];
for k = 1 : segNum
segment(k,:) = [prePt(k), postPt(k)];
end

Accepted Answer

Stephen23
Stephen23 on 12 Jan 2016
Edited: Stephen23 on 12 Jan 2016
That bit of code concatenates zeroes onto both ends of the vector idx, and allocates this lengthened vector to a new variable idx2. Like this:
>> X = [5,6,7];
>> [0,X,0]
ans = 0 5 6 7 0
This is because the [] is a concatenation operator (not a list generation operator like lots of beginners think). Read this for more info on []:
  9 Comments
Animesh Shaw
Animesh Shaw on 13 Jan 2016
one more thing sir pls make me understand this line >>segment(k,:) = [prePt(k), postPt(k)] what output it will give.
Thank you so much for giving time and helping me out and making me understand things
Stephen23
Stephen23 on 13 Jan 2016
Edited: Stephen23 on 13 Jan 2016
In my answer I explained that [] is a concatenation operator. This means that the code [prePt(k),postPt(k)] concatenates the k-th element of prePt and postPt together, and allocates this to the k-th row of segment.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!