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

2 visualizzazioni (ultimi 30 giorni)
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)
  1 Commento
Jason
Jason il 16 Ott 2024
To my later added 2nd part of the question, stephen kindly pointed me to:
[W,Y2] = maxk(DD,5,ComparisonMethod="abs")

Accedi per commentare.

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by