6 views (last 30 days)

I got a good answer to my question:

Here is my MATLAB version:

MATLAB Version 9.8 (R2020a)

Communications Toolbox Version 7.3 (R2020a)

DSP System Toolbox Version 9.10 (R2020a)

Instrument Control Toolbox Version 4.2 (R2020a)

LTE Toolbox Version 3.3 (R2020a)

MATLAB Compiler Version 8.0 (R2020a)

Parallel Computing Toolbox Version 7.2 (R2020a)

RF Toolbox Version 3.8 (R2020a)

Signal Processing Toolbox Version 8.4 (R2020a)

Symbolic Math Toolbox Version 8.5 (R2020a)

But I neglected to say that I use dY_idx and Y_adj_close in a subsequent function call where both arguments must have the same dimensions.

Here is my code that keeps the dimensions the same. Is there any way to improve this code to make it run faster (for large vectors)?

Y = [ 1.1 1.12 9.2 8.3 8.295 8.292 4.1 4.12 4.19];

plot(Y), hold on;

% Original 2 lines of code that loses the right-most point is:

% dY_idx = find(abs(diff(Y)) < 0.022);

% Y_adj_close = Y(dY_idx)

% Here are my 5 lines of code that saves the right-most point to keep the right-most point of each grouping.

% Can these 5 lines be reduced?

logidx = abs(diff(Y)) < 0.022;

xLog = [logidx 0] | [0 logidx];

Y_adj_close = Y( xLog );

dY_idx = 1:length(Y);

dY_idx = dY_idx( xLog );

plot(dY_idx, Y_adj_close, 'r.'), hold off

Tommy
on 10 Jun 2020

logidx = abs(diff(Y)) < 0.022;

dY_idx = find([logidx 0] | [0 logidx]);

Y_adj_close = Y( dY_idx );

Seems to me that every part of these three lines is required, but maybe it can be made even faster.

Of course number of lines alone isn't the only thing to consider. I'd imagine this would be slower:

dY_idx = find([abs(diff(Y)) < 0.022 0] | [0 abs(diff(Y)) < 0.022]);

Y_adj_close = Y( dY_idx );

KSSV
on 8 Jun 2020

Walter Roberson
on 9 Jun 2020

The file exchange has https://www.mathworks.com/matlabcentral/fileexchange/4586-k-d-tree and several other KDTree implementations.

However, if you are working in 1D then it might not be worth it, but interp1() 'nearest' might possibly be faster (I think it calls into mex code.)

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

Start Hunting!
## 6 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_888296

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_888296

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_888302

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_888302

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_889331

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_889331

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_889340

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_889340

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_889523

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_889523

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_889526

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/543197-plot-only-adjacent-points-that-are-close-to-each-other#comment_889526

Sign in to comment.