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

1 view (last 30 days)
Dear all,
I have six vectors, three with peak values and three vectors with associated location values based on three peak analysis.
% Sensor 1 (15 events)
loc1=[1213 1373 1795 2659 4348 5020 5491 5667 8537 8953 9173 10536 10826 10991 11489];
pks1=[404 998 891 1844 329 428 1025 335 2354 549 997 438 7531 614 545];
% Sensor 2 (14 events)
loc2=[1373 1795 2659 3051 4348 5020 5491 5667 8537 8953 9173 10826 10991 11489];
pks2=[1372 1065 1327 323 568 461 989 504 1322 532 440 7650 566 484];
% Sensor 3 (11 events)
loc3=[1373 1795 2659 4348 5491 8537 8953 9173 10826 10991 11489];
pks3=[1547 920 931 601 1058 1628 652 638 5332 741 397];
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.
  • 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 output of the new vector shall be:
new_vector=[404 1547 1065 1844 323 601 461 1058 504 2354 652 997 438 7650 741 545]
I have attached a plot illustrating the identified events from the different sensors.
If anything is unclear please let me know!
Thanks in advance.
  4 Comments
Askeladden2
Askeladden2 on 1 Oct 2019
Darova,
If I concatenate all the vector values, I'll end up with two vectors, one with all events and the other with peaks.
A, sorted events.
A=[1213 1373 1373 1373 1795 1795 1795 2659 2659 2659 3051 4348 4348 4348 5020 5020 5491 5491 5491 5667 5667 8537 8537 8537 8953 8953 8953 9173 9173 9173 10536 10826 10826 10826 10991 10991 10991 11489 11489 11489];
B, associated peaks.
B=[404 1547 1372 998 920 1065 891 1503 1844 1627 323 329 568 601 428 461 1025 1010 1058 335 504 2354 1473 1628 652 556 549 997 440 638 438 5332 7650 7531 741 596 614 545 545 457 ]
Is it possible to use a loop of vector A in order to get the maximum value of vector B, when vector has the same (time+-2s)?
I.e. If A(x).. <= (A(x+1)+-2)
C(x)=max(A(x),A(x+1);
else
C(x)=(A(x);
Example:
For example is A(1,1) unique and therefore C=B(1,1).
For A(2,1) this is equal (+-2s) to A(3,1) and A(4,1). Therefore C=max(B(2,1),B(3,1),B(4,1))
I am not sure if this is the easiest way or even possible..
darova
darova on 1 Oct 2019
Sure it's possible
C = A; % preallocate array
for i = 1:length(A)-1
if abs(A(i) - A(i+1)) < 2
C(i) = max( A(i), A(i+1) );
end
end
But another part of your task remains unclear for me

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 1 Oct 2019
Edited: the cyclist on 1 Oct 2019
% Sensor 1 (15 events)
loc{1}=[1213 1373 1795 2659 4348 5020 5491 5667 8537 8953 9173 10536 10826 10991 11489];
pks{1}=[404 998 891 1844 329 428 1025 335 2354 549 997 438 7531 614 545];
% Sensor 2 (14 events)
loc{2}=[1373 1795 2659 3051 4348 5020 5491 5667 8537 8953 9173 10826 10991 11489];
pks{2}=[1372 1065 1327 323 568 461 989 504 1322 532 440 7650 566 484];
% Sensor 3 (11 events)
loc{3}=[1373 1795 2659 4348 5491 8537 8953 9173 10826 10991 11489];
pks{3}=[1547 920 931 601 1058 1628 652 638 5332 741 397];
all_loc = [loc{1} loc{2} loc{3}];
all_pks = [pks{1} pks{2} pks{3}];
num_loc = numel(all_loc);
single_loc = [];
single_pks = [];
for ni = 1: num_loc
[isCloseLoc, idxClose] = ismembertol(all_loc(ni),single_loc,2,"DataScale",1);
if isCloseLoc
single_pks(idxClose) = max(single_pks(idxClose),all_pks(ni));
else
single_loc = [single_loc, all_loc(ni)];
single_pks = [single_pks, all_pks(ni)];
end
end
figure
hold on
for nc = 1:3
plot(loc{nc},pks{nc},'.')
end
figure
hold on
for nc = 1:3
plot(single_loc,single_pks,'.')
end
Note that I used a cell array instead of dynamically named variables.
Screen Shot 2019-10-01 at 4.02.05 PM.png
Screen Shot 2019-10-01 at 4.02.14 PM.png

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!