How to search an compare two arrays for certain criteria

3 次查看(过去 30 天)
I have to arrays that are the same length with similar but not identical values. I need to compare from start to finish these arrays to see when array A is greater than or less than array B by 10% +/- 20. Once I find when those instance occur I need to plot data points 5 seconds before and 5 seconds after the event. Should I use an if and statement loop? Any help is appreciated, thanks.
  2 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2014-1-3
This is not clear, give a numeric example and post the expected result
Barnabas
Barnabas 2014-1-3
编辑:Barnabas 2014-1-3
Apologies, here is an example below:
A = [100 115 200]
B = [111 105 150]
I need to compare A(1) with B(1) and see if they are within 10% +/- 20. If the difference is greater than 10% +/- 20 then I need to mark that index position, and plot 5 data-points before and after the event.
So I would like a new array that would populate at what index points (in this case A(3)) the difference is greater than 10% +/- 20. I hope that helps. Below is what I have o far, but am not confident in the for loop statements accuracy.
TqRspns = rot90(EngTrqStatic);
TqRqst = (EngTrq_Rq_ESC);
% P = Percent Difference Requested for sort %
P = 10;
% PM = Plus/Minus Nm value for secondary sort %
PM = 20;
n = 1;
TqRqst1 = (interp1(TqRqst,1:0.50032725075316337285902503293808:126077));
Difference = abs(((TqRspns./TqRqst1)*100)-100);
index = find(Difference>P);
for n = 1:251988;
if (TqRqst1(n)>TqRspns(n)+PM);
Found(n) = n;
n+1;
end;
end;

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2014-1-3
mask = (A > 0.9 * B - 20) & (A < 1.1 * B + 20);
extended_mask = logical(filter(mask, ones(1,11)));
Now extended_mask selects the elements to be plotted. How to proceed with the actual plotting depends on what you want the output to look like when there are multiple events.

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by