亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript從數組中的高點組中挑選出峰值數

Javascript從數組中的高點組中挑選出峰值數

開心每一天1111 2021-06-30 05:18:51
很難用標題來描述它,但從這里的描述和代碼中應該很清楚。我試圖從一組峰值中挑選出數組中的最高數字,然后進入下一組并從下一組高點中獲取下一個峰值數。所以例如在這個數組中:const arr = [939, 1301, 253, 1380, 1037, 2279, 2462, 2193, 2121, 1424, 506, 2411, 2456, 2295, 915, 1276, 1532, 1359, 985, 2182, 2407, 2103, 2392, 2294, 765, 1195, 1537, 1409, 858, 1971, 2214, 1311, 1326, 1383, 1231, 1141]我想2462從大約 的第一組峰值中1037, 2279, 2462, 2193挑選出來,然后繼續2456從大約 的下一組中挑選出來506, 2411, 2456, 2295, 915。這些組的位置未知,只需找到它們并從每個組中選擇一個最高的數字即可。這是我目前擁有的功能,但它不能 100% 工作并且不適用于某些數組。let currHigh = 0, store = [], limit = 2100;for (let s = 0; s < arr.length; s++) {    if (arr[s] > limit && currHigh === 0) {        currHigh = arr[s]    } else if (arr[s] > limit && arr[s] > currHigh) {        currHigh = arr[s];    } else if (arr[s] < currHigh) {        if (arr[s] < limit) {            store.push(currHigh); currHigh = 0;        }    }}console.log(store)高點總是非常相似(2000+),數字組的位置相似但不一樣,所以我不能真正依賴它。所以第一個數組和預期/期望的輸出是:const arr = [939, 1301, 253, 1380, 1037, 2279, 2462, 2193, 2121, 1424, 506, 2411, 2456, 2295, 915, 1276, 1532, 1359, 985, 2182, 2407, 2103, 2392, 2294, 765, 1195, 1537, 1409, 858, 1971, 2214, 1311, 1326, 1383, 1231, 1141]Outputted Peaks: [2462, 2456, 2407, 2294, 2214]另一個具有預期/所需輸出的示例數組:const arr =  [1365, 1324, 1013, 1220, 1259, 2204, 2212, 1938, 1882, 1545, 1236, 2090, 2614, 1949, 1307, 1628, 1780, 1263, 1184, 2184, 1411, 1306, 2010, 2057, 1339, 1624, 2480, 2575, 2425, 2617, 2479, 1929, 1805, 1869, 1341, 1104, 2195, 1661, 1174, 1447, 1761, 1362, 1430]Outputted Peaks: [2212, 2614, 2184, 2480, 2617, 2195]編輯:為了避免混淆,想象一下,如果您要獲取給定數組中的值并將它們繪制在圖形上,圖形中會有幾個相互隔開的峰值。我需要一個函數來獲得這些峰值。查找組只是查找峰值的一種方法。它們就像迷你山。因此,例如,您不會希望獲得位于“山的一側”的高數字。編輯:附加圖像來解釋峰值。緊挨著另一個高數的高數不是峰,只有一小組相鄰數中的最高數才是峰。 最終編輯:此圖最好地說明了它,因為很難想象僅查看數組的圖:https://jsbin.com/nicuciquru/1/edit?js,output所以我在這里需要的是[1301, 1380, 2462, 2456, 1532, 2407, 2492, 1537, 2214, 1383]你可以在圖表上看到的峰值。然后從那里可以使用閾值輕松過濾這些峰值,例如消除低于 2000 的任何峰值。
查看完整描述

3 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

這是一個查找樣本數組中所有峰值的函數:


    const chart = [939, 1301, 253, 1380, 1037, 2279, 2462, 2193, 2121, 1424, 506, 2411, 2456, 2295, 915, 1276, 1532, 1359, 985, 2182, 2407, 2103, 2392, 2294, 765, 1195, 1537, 1409, 858, 1971, 2214, 1311, 1326, 1383, 1231, 1141]


    const findPeaks = chart => chart.reduce((agg, current, i) => {

        const prev = chart[i - 1];

        const next = chart[i + 1];


        return current > prev && current > next ? [...agg, current] : agg;

    }, []);


    const peaks = findPeaks(chart);


    console.log('peaks', peaks);

這記錄: [1301, 1380, 2462, 2456, 1532, 2407, 2392, 1537, 2214, 1383]


這與您的問題的預期輸出不匹配,但它確實對“峰值是什么”給出了準確的答案。


在這種情況下,“峰值”被定義為任何前任和后繼都低于自身的數字。即 [1, 100, 1] 給出 100。 [1, 2, 3, 100, 3, 2, 100, 1] 給出 [100, 100]


更新


根據您的反饋,這里有一個函數,它需要一個峰的每邊距離為 2 。結果仍然與您的問題不同,但這符合新表達的標準:


const findPeaks = chart => chart.reduce((agg, current, i) => {

        const twoBack = chart[i - 2];

        const prev = chart[i - 1];

        const next = chart[i + 1];

        const twoForward = chart[i + 2];


        return (twoBack < prev && prev < current) && (current > next  && next > twoForward ) 

        ? [...agg, current] 

        : agg;

    }, []);

對于上述輸入,這會產生:


[2462, 2456, 1532, 1537, 1383]


更新 2


這是找到所有峰值、計算出中值并返回大于中值的所有峰值的代碼版本。這是與您在問題中要求的最接近的結果:


const findPeaks = chart => {


    const peaks = chart.reduce((agg, current, i) => {

    const twoBack = chart[i - 2];

    const prev = chart[i - 1];

    const next = chart[i + 1];

    const twoForward = chart[i + 2];


    return (prev < current) && (current > next) 

    ? [...agg, current] 

    : agg;

    }, []);


    const mean = peaks.reduce((a, b) => a + b) / peaks.length;


    return peaks.filter(peak => peak > mean);

}

這個的輸出是:


[2462, 2456, 2407, 2392, 2214]


查看完整回答
反對 回復 2021-07-01
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

沒有更精確地定義所需的輸出(例如,什么被認為是一個組?),這是一個簡單的算法:


1) 將數組拆分為“低”和“高”序列。


例如[1, 10000, 100, 101, 20000, 20050, 30, 10, 2]=> [1], [10000], [100, 101], [20000, 20050], [30, 10, 2]。

2) 返回每個高序列的最大值。


let peaks = arr => {

    let isHigh = a => a >= 2000;

    let isLow = a => !isHigh(a);


    let groups = [];

    while (arr.length && arr.some(isHigh)) {

        arr.splice(0, arr.findIndex(isHigh));

        groups.push(arr.splice(0, arr.findIndex(isLow)));

    }


    return groups.map(group => Math.max(...group));

};



const arr = [939, 1301, 253, 1380, 1037, 2279, 2462, 2193, 2121, 1424, 506, 2411, 2456, 2295, 915, 1276, 1532, 1359, 985, 2182, 2407, 2103, 2392, 2294, 765, 1195, 1537, 1409, 858, 1971, 2214, 1311, 1326, 1383, 1231, 1141];

console.log(peaks(arr));


const arr2 = [1365, 1324, 1013, 1220, 1259, 2204, 2212, 1938, 1882, 1545, 1236, 2090, 2614, 1949, 1307, 1628, 1780, 1263, 1184, 2184, 1411, 1306, 2010, 2057, 1339, 1624, 2480, 2575, 2425, 2617, 2479, 1929, 1805, 1869, 1341, 1104, 2195, 1661, 1174, 1447, 1761, 1362, 1430];

console.log(peaks(arr2));


查看完整回答
反對 回復 2021-07-01
?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

為什么不保持簡單?


var arr = [1,6,4,3,9,11];


var peaks = arr.sort((a, b) => a - b).reverse();

輸出為:[11, 9, 6, 4, 3, 1]


然后:


var top5 = peaks.slice(0,5);

結果是:[11, 9, 6, 4, 3]


或者你的數學的整個代碼:


const peaks1 = [939, 1301, 253, 1380, 1037, 2279, 2462, 2193, 2121, 1424, 506, 2411, 2456, 2295, 915, 1276, 1532, 1359, 985, 2182, 2407, 2103, 2392, 2294, 765, 1195, 1537, 1409, 858, 1971, 2214, 1311, 1326, 1383, 1231, 1141];

const peaks2 =  [1365, 1324, 1013, 1220, 1259, 2204, 2212, 1938, 1882, 1545, 1236, 2090, 2614, 1949, 1307, 1628, 1780, 1263, 1184, 2184, 1411, 1306, 2010, 2057, 1339, 1624, 2480, 2575, 2425, 2617, 2479, 1929, 1805, 1869, 1341, 1104, 2195, 1661, 1174, 1447, 1761, 1362, 1430];

const peaks = [peaks1, peaks2];

function getPeaks(arr){

    let peaks = [];

    for(let i=0;i<arr.length;i++){

        let currPeak = arr[i].sort((a, b) => a - b).reverse().slice(0,1)[0];

        peaks.push(currPeak);

    }

    return peaks.sort((a, b) => a - b).reverse();

}

let result = getPeaks(peaks);

結果應該是:[2617, 2462]


查看完整回答
反對 回復 2021-07-01
  • 3 回答
  • 0 關注
  • 345 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號