can any one tell me how this code is working??

1 view (last 30 days)
can any one tell me how this code is working ..I cant able to understand the logic ..pls help.(Please note that locs_Rwave will be always higher than locs_Qwave)
q=0;
k=1;
locs_Rwave = [116 110 57]';
locs_Qwave=[110 210 356 457 575 313]';
for j=k:size(locs_Rwave)
for i=1:numel(locs_Qwave)
if (i== numel(locs_Qwave))
q=[q locs_Qwave(i)];
break;
end
if( locs_Qwave(i)>locs_Rwave(j))
q=[q locs_Qwave(i-1)];
break;
end
end
end
q
Thanks in advance

Answers (1)

Roger Stafford
Roger Stafford on 15 Sep 2014
This does not seem like good code to me. It is likely that what it does is not what its author intended.
For example, if one of the 'locs_Rwave' elements happens to be less than the first element of 'locs_Qwave' (which is not true in this particular case,) then when j indexes that element of 'locs_Rwave' and when the inner loop sets i = 1, there would be an attempted access to locs_Qwave(0) which would result in an error message. One can only speculate as to what was intended here.
Also the line
for j=k:size(locs_Rwave)
is strange because size(locs_Rwave) is a two-element vector. Fortunately for the author the colon operator apparently ignores the second part of it, but why didn't the author write a more straightforward size(locs_Rwave,1) or numel(locs_Rwave)?
Furthermore, I would guess that q was meant to be empty at the beginning instead of being set to the scalar quantity zero. If so, the proper code should have been "q = [];". That zero will persist as locs_Qwave elements are appended to q.
  2 Comments
Abhishek sadasivan
Abhishek sadasivan on 15 Sep 2014
locs_Rwave will always a higher value than locs_Qwave..(R wave of ECG data).
Roger Stafford
Roger Stafford on 15 Sep 2014
Edited: Roger Stafford on 15 Sep 2014
That's not true in the example you have given above:
locs_Rwave = [116 110 57]';
locs_Qwave = [110 210 356 457 575 313]';
For these values, only the fact that 57 is the last element in locs_Rwave saves this code from issuing an error message about locs_Qwave(0).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!