Finding max value of 1D vector when all values could be negative

2 次查看(过去 30 天)
Hello, I have a set of numbers and want to identify the largest difference between adjacent values. I have used the diff function and this returns
DD=diff(D) % D is my data,and can be postive or negative!!
DD =
-0.8867
-0.9328
-0.9667
-33.9785
-0.1568
-0.1511
-0.1434
-0.1334
So to identify the largest index and then value, I have to allow for negative values of DD as above - hence the abs below.
This was my 1st attempt, but it diodnt work as the try part didn'f fail.
m=max(abs(DD(:))); % Need the abs to allow for any sign of data
try
idx=find(DD==m)
catch
idx=find(DD==-m)
end
So I tried this which did work: Is there a better way
idx=find(DD==m)
[sy,sx]=size(idx)
if sy==0
idx=find(DD==-m)
end
I then want to list out the highest say 4 values of the differences DD, but again as the signs can be =ve or -ve, I can use the normal sort with ascend / descend, how to do this?
This was my attempt:
X=data(idx,1);
Y=data(idx,col);
sortedDiff=sort(DD,'descend');
sortedDiff(1:5,1)

采纳的回答

Alan Stevens
Alan Stevens 2024-10-16
Try
[M, I] = max(abs(DD))
M = DD(I)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by