Extract maximum values from two different vectors when their associated location vectors crosses

1 visualización (últimos 30 días)
Dear all,
I have four vectors, two with peak values and two vectors with associated location values based on two peak analysis.
loc1=[10.4 15.8 29.2 32.1 35.9];
loc2=[10.1 15.2 35.6];
pks1=[10 1 5 4 100];
pks2=[8 5 1000];
I want create a new vector with the maximum value from vector pks1 and from pks2 when loc1 and loc2 have the approximately same value (~+-2).
I.e. loc1 and loc2 cross at position 1,2 and 5 (10.1 and 10.4, 15.2 and 15.8, 35.6 and 35.9), so my new vector will be: final=[10 5 5 4 1000].
Can anyone help me out?
Thanks in advance!
  6 comentarios
Adam Danz
Adam Danz el 29 de Sept. de 2019
Finding the max pks values for loc values that are within 2 units is easy. It's just unclear what to do with the unpaired loc values.
% Find indices of loc1 & loc2 that are within 2 units of each other
% locsAreClose(n,m) determines if loc1(n) and loc2(m) are within +/-2
locsAreClose = abs(loc1(:)-loc2(:).')<=2; %rows@loc1, cols@roc2
% Find the max between all pks pairs
maxPairs = max(pks1(:),pks2(:).');
% Output the max for each pair
out = maxPairs(locsAreClose);
Adam Danz
Adam Danz el 29 de Sept. de 2019
Per Vatsvag's answer moved here as a comment.
Thanks for all replies
The cyclist: Yes ,the algorithm you describe is correct!
Adam Danz: In principle I need to account for all peaks in pks1, but also considered that the maximum peak force may occur in the second sensor (pks2) for an event where both the sensors are triggered. The pks1-sensor have triggered 5 times and the pks2-sensor have triggered 3 times, which means that my new vector must contain 5 values. Since the second sensor have triggered at the same time (+-2s) as the first sensor (pks1) then I want to use the maximum of those.

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 29 de Sept. de 2019
Editada: Adam Danz el 29 de Sept. de 2019
"I need to account for all peaks in pks1, but also considered that the maximum peak force may occur in the second sensor (pks2)"
Just a small adjustment is needed in the code from my comment under your question.
loc1=[10.4 15.8 29.2 32.1 35.9];
loc2=[10.1 15.2 35.6];
pks1=[10 1 5 4 100];
pks2=[8 5 1000];
% Find indices of loc1 & loc2 that are within 2 units of each other
% locsAreClose(n,m) determines if loc1(n) and loc2(m) are within +/-2
locsAreClose = abs(loc1(:)-loc2(:).')<=2; %rows@loc1, cols@roc2
% Find the max between all pks pairs
maxPairs = max(pks1(:),pks2(:).');
% Output
pksMax = pks1;
pksMax(any(locsAreClose,2)) = maxPairs(locsAreClose);
[update]
For n peak/loc vectors, just combine the vectors except for loc1 and pks1. So, this can work for any number of loc/pks vectors.
loc1=[10.4 15.8 29.2 32.1 35.9];
loc2=[10.1 15.2 35.6];
loc3=[15.3 29 35.6 44];
pks1=[10 1 5 4 100];
pks2=[8 5 1000];
pks3=[7 6 50 90];
loc23 = [loc2, loc3]; % combine them into 1 vector
pks23 = [pks2, pks3]; % combine them into 1 vector
% Find indices of loc1 & loc23 that are within 2 units of each other
locsAreClose = abs(loc1(:)-loc23(:).')<=2; %rows@loc1, cols@loc23
% Find the max between all pks pairs
maxPairs = max(pks1(:),pks23(:).');
maxPairs(~locsAreClose) = -inf;
maxVals = max(maxPairs,[],2); % a vector of all max values
% Output
pksMax = pks1;
pksMax(any(locsAreClose,2)) = maxVals(any(locsAreClose,2));
  2 comentarios
Adam Danz
Adam Danz el 29 de Sept. de 2019
Per Vatsvag's answer moved here as a comment.
Adam Danz:Thank you for the swift reply and excellent help!
You have just saved me for a ton of manual work!
Adam Danz
Adam Danz el 29 de Sept. de 2019
Editada: Adam Danz el 29 de Sept. de 2019
Glad I could help! Thanks to the cyclist too for helping to define the requirements.

Iniciar sesión para comentar.

Más respuestas (2)

the cyclist
the cyclist el 29 de Sept. de 2019
% The data
loc1=[10.4 15.8 29.2 32.1 35.9];
loc2=[10.1 15.2 35.6];
pks1=[10 1 5 4 100];
pks2=[8 5 1000];
% Find the values of loc2 that are close to loc1, and their corresponding
% indices
[isClose,closeIndex] = ismembertol(loc2,loc1,2,'DataScale',1);
% Define the initial guess of the true peaks to be all the pks1 values
truePeaks = pks1;
% Overwrite the close ones with pks2
truePeaks(closeIndex(isClose)) = pks2(isClose);
  3 comentarios
Adam Danz
Adam Danz el 29 de Sept. de 2019
Editada: Adam Danz el 29 de Sept. de 2019
It's not as simple as that. loc1(2) is close to loc2(2) and loc3(1) and for this data, the pks3(1) value is greatest so your solution works. Change the pks3(1) value so that it's less than pks(2) and the solution no longer works.
% v-- now pks2(2) > pks3(1)
pks2=[8 7 1000];
pks3=[5 6 50 90];

Iniciar sesión para comentar.


Askeladden2
Askeladden2 el 29 de Sept. de 2019
If I wish to extend this problem to three variables (/sensors),
E.g.:
loc1=[10.4 15.8 29.2 32.1 35.9];
loc2=[10.1 15.2 35.6];
loc3=[15.3 29 35.6 44];
pks1=[10 1 5 4 100];
pks2=[8 5 1000];
pks3=[7 6 50 90]
Any suggestions?
Also, thank you the cyclist for your answer!
  6 comentarios
Adam Danz
Adam Danz el 30 de Sept. de 2019
ps, the biggest lesson to learn here (and one that most of us learned the hard way) is to have a well planned set of rules before the first line of code is written. That's not to say that you won't need to make small adjustments as you go along. But following a planned sketch is much easier than sketching it out as you go. I'd bet that every 1 minute spent planning an algorithm before implementing it can save 15-30 minutes of troubleshooting and re-writing.
Askeladden2
Askeladden2 el 30 de Sept. de 2019
Adam and the cyclist:
Thank you for all your help so far.
The problem has changed, but is related, so instead of making a new threat I thought it would be easier just to continue on this one.
The following algorithm applies:
  • First, select all of the peaks defined by pks1, pks2 and pks3.
  • Next, identify the maximum value from pks1,pks2 and pks3 for each "event". The events are given by the location vector in time and shall be defined as "same event" when the time cross +-2 seconds. I.e. loc1(2) and loc2(1) ~1373s.
  • The new vector shall contain all the maximum values from all the identified events. 16 events have been identified by comparing the values in the 3 loc-vectors.
  • There may be 1, 2 or 3 peak values in one event.
Example: Only one peak value in an event. I.e. new vector must include 323.
Sensor 2: loc2(4),pks2(4) / time=3051s and peak=323
Example: Two peak values in an event. I.e. new vector must include 1547.
Sensor 2: loc2(1),pks2(1) / time=1373s and peak=1372
Sensor 3: loc3(1),pks3(1) / time=1373s and peak=1547
Example: Three peak values in an event. I.e. new vector must include 1065.
Sensor 1: loc1(3),pks1(3) / time=1795s and peak=891
Sensor 2: loc2(2),pks2(2) / time=1795s and peak=1065
Sensor 3: loc3(2),pks3(2) / time=1795s and peak=920
  • The new vector shall contain 16 values given the number of events.
I have attached a plot illustrating the identified events from the different sensors.
If anything is unclear please let me know!
Thanks again for all help.
I will comment on questions/answers accordingly (blame a new user) from now on.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by