Asked by Maxwell Roth
on 19 Jul 2013

First I'd like to thank everyone who answers these posts. I've found this forum really useful when looking up a solution to a problem.

Ok here's an example situation.

given a rather chaotic waveform D and its time vector T

I want to find the value of D where the slope is say at a maximum.

Slopes = diff(D)./diff(T) %so I would take the derivative MaxSlope = max(Slopes) %find the max

now I want to find the value in D where that slope occurs. Is there a more elegant way of doing this than?

for index = 1:length(Slopes) if Slopes(index) == MaxSlope; Point = index end end Value = D(Point)

Such as some way of finding out at what point in Slopes MaxSlope occurs?

*No products are associated with this question.*

Answer by dpb
on 19 Jul 2013

Accepted Answer

Sure...

Slopes = diff(D)./diff(T); [MaxSlope,ix] = max(Slopes); vMxSlope=D(ix); % duplicates loop wrt boundary condition

Might want to use ix+1 for the point owing to the reduction in length from the diff() operation; your call.

doc max

Also if the time sampling is uniform can eliminate one diff() on the whole vector and use dT=T(2)-T(1). Of course, if isn't uniform then the above is needed. Probably makes little, if any noticeable difference unless very long time series are involved.

Maxwell Roth
on 19 Jul 2013

Awesome! I imagine Mean and Min have this too. Is there anything more general? Something you could use to find the index where any number occurs in a vector?

Image Analyst
on 20 Jul 2013

mean() does not, for obvious reasons, since the mean is not required to be in the array like min and max are. min() and max() both return the extreme value plus the index(es) where it occurs. To find a general value, you must use the FAQ to look within a tolerances. http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F or else look for the closest value

index = find(sort(abs(data - soughtValue), 'ascend'), 1, 'first');

Maxwell Roth
on 22 Jul 2013

This is great thanks, and you're absolutely right; in a general case you can't assume that the value actually appears in the data vector.

Log in to comment.

Answer by Image Analyst
on 19 Jul 2013

Try this:

Slopes = diff(D)./diff(T) [maxSlope, indexOfMaxSlope] = max(Slopes)

Log in to comment.

Answer by per isakson
on 20 Jul 2013

Edited by per isakson
on 20 Jul 2013

Logical indexing is one of my hammers, thus I saw a nail;-)

%% N = 1e4; Slopes = randn( N, 1 ); %% tic, Point1 = max(Slopes); ixPoint1 = find( Slopes==Point1 ); toc %% tic, [ Point2, ixPoint2 ] = max( Slopes ); toc

the fourth execution returned

Elapsed time is 0.000087 seconds. Elapsed time is 0.000036 seconds.

No doubt which construct is more elegant!

BTW: The tooltip help (Cntrl+F1) doesn't show the output arguments, only the inputs. It's too easy not to remember and overlook the second and third outputs.

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 2 Comments

## per isakson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/82633-elegant-way-to-return-point-a-specific-value-occurs-at-in-a-vector#comment_160743

Try something like this

## Image Analyst (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/82633-elegant-way-to-return-point-a-specific-value-occurs-at-in-a-vector#comment_160746

per, why not move this to an answer so you'll get credit.

Log in to comment.