Diff Two Arrays-freecodecamp算法题目

Diff Two Arrays(比较两个数组)


1.要求

  • 比较两个数组,然后返回一个新数组
  • 该数组的元素为两个给定数组中所有独有的数组元素。换言之,返回两个数组的差异。

2.思路

  • 定义一个新数组变量,将输入的两个数组用.concat()连接到一起赋值给它
  • 定义一个check函数,返回两个给定数组中所有独有的数组元素
  • 用.filter()筛选出新数组变量的符合条件的元素

3.代码

function diff(arr1, arr2) {
var newArr = [];
newArr = arr1.concat(arr2);
function check(item){
    if(arr1.indexOf(item) === -1 ||arr2.indexOf(item) === -1){
    return item;
    }
}
return newArr.filter(check);
}
diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);

4.相关链接

5.博客说明:以后的博客均采用mardown语法编写

转载于:https://www.cnblogs.com/ahswch/p/9297849.html

以下是寻峰算法的Matlab代码: function [peaks, locs] = findpeaks(x, varargin) % FINDPEAKS Find peaks in a vector % [PEAKS, LOCS] = FINDPEAKS(X) returns the values and locations of the % peaks of the input vector X. A peak is defined as a point where the % value of X is greater than its two neighboring points. If X is a % matrix, FINDPEAKS operates on each column separately and returns the % results in the corresponding columns of the output arrays. % % [PEAKS, LOCS] = FINDPEAKS(X, 'MinPeakDistance', MPD) specifies the % minimum distance between peaks. This parameter is specified in % number of samples. The default value is 1. % % [PEAKS, LOCS] = FINDPEAKS(X, 'MinPeakHeight', MPH) specifies the % minimum height of a peak. This parameter is specified as a fraction % of the maximum value of X. The default value is 0.5. % % [PEAKS, LOCS] = FINDPEAKS(X, 'Threshold', TH) specifies a % threshold value. Only peaks above this threshold will be detected. % This parameter is specified as a fraction of the maximum value of X. % The default value is 0. % % [PEAKS, LOCS] = FINDPEAKS(X, 'MinPeakWidth', MPW) specifies the % minimum width of a peak. This parameter is specified in number of % samples. The default value is 1. % % Example: % x = -4:0.01:4; % y = sin(x) + 0.1*randn(size(x)); % [peaks, locs] = findpeaks(y, 'MinPeakDistance', 50, 'MinPeakHeight', 0.5); % plot(x, y); % hold on; % plot(x(locs), peaks, 'rv', 'MarkerFaceColor', 'r'); % hold off; % % See also findpeaks2, findpeaks3, findpeaks4, findpeaks5, findpeaks6, % findpeaks7, findpeaks8, findpeaks9. % Copyright 2019 The MathWorks, Inc. % Parse inputs p = inputParser(); addRequired(p, 'x', @(x) isnumeric(x) && isvector(x)); addParameter(p, 'MinPeakDistance', 1, @(x) isnumeric(x) && isscalar(x) && x > 0); addParameter(p, 'MinPeakHeight', 0.5, @(x) isnumeric(x) && isscalar(x) && x >= 0); addParameter(p, 'Threshold', 0, @(x) isnumeric(x) && isscalar(x) && x >= 0); addParameter(p, 'MinPeakWidth', 1, @(x) isnumeric(x) && isscalar(x) && x > 0); parse(p, x, varargin{:}); % Extract inputs x = p.Results.x(:); mpd = p.Results.MinPeakDistance; mph = p.Results.MinPeakHeight; th = p.Results.Threshold * max(x); mpw = p.Results.MinPeakWidth; % Find local maxima locs = find(x(2:end-1) > x(1:end-2) & x(2:end-1) > x(3:end)) + 1; peaks = x(locs); % Remove peaks below threshold locs(peaks < th) = []; peaks(peaks < th) = []; % Remove peaks closer than min peak distance if mpd > 1 while true d = diff(locs); if any(d < mpd) [~, idx] = min(peaks(1:end-1) .* (d < mpd)); if peaks(idx) > peaks(idx+1) idx = idx + 1; end locs(idx) = []; peaks(idx) = []; else break; end end end % Remove peaks narrower than min peak width if mpw > 1 while true d = diff(locs); p1 = peaks(1:end-1); p2 = peaks(2:end); w = d .* (p1 + p2) / 2; if any(w < mpw) [~, idx] = min(w); if peaks(idx) > peaks(idx+1) idx = idx + 1; end locs(idx) = []; peaks(idx) = []; else break; end end end end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值