Elegant way to return point a specific value occurs at in a vector
古いコメントを表示
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?
2 件のコメント
per isakson
2013 年 7 月 19 日
Try something like this
Point = find( Slopes==MaxSlope );
Image Analyst
2013 年 7 月 19 日
per, why not move this to an answer so you'll get credit.
採用された回答
その他の回答 (2 件)
Image Analyst
2013 年 7 月 19 日
Try this:
Slopes = diff(D)./diff(T)
[maxSlope, indexOfMaxSlope] = max(Slopes)
per isakson
2013 年 7 月 20 日
編集済み: per isakson
2013 年 7 月 20 日
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.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!