Extract maximum values from two different vectors when their associated location vectors crosses
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Adam Danz
2019년 9월 29일
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
2019년 9월 29일
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.
채택된 답변
Adam Danz
2019년 9월 29일
편집: Adam Danz
2019년 9월 29일
"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
Adam Danz
2019년 9월 29일
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!
추가 답변 (2개)
the cyclist
2019년 9월 29일
% 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
Adam Danz
2019년 9월 29일
편집: Adam Danz
2019년 9월 29일
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];
Askeladden2
2019년 9월 29일
댓글 수: 6
Adam Danz
2019년 9월 30일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!