Finding local minima about either side of a maxima in a matrix in matlab












1















For some "big data" processing. When plotted my data is sign wave like but with random peaks. (So imagine plotting the value of each matrix position against it's position)



I know how to find the peaks, but I need a way of then finding the value of local minima either side of the peaks and the position in the matrix. For example, if the data were:



3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0



The function I need would return something like: min,loc = [0, -6; 4, 11]










share|improve this question























  • So if you know how to find peaks in a vector x, to find the minima just find the peaks of -x.

    – obchardon
    Nov 14 '18 at 14:09
















1















For some "big data" processing. When plotted my data is sign wave like but with random peaks. (So imagine plotting the value of each matrix position against it's position)



I know how to find the peaks, but I need a way of then finding the value of local minima either side of the peaks and the position in the matrix. For example, if the data were:



3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0



The function I need would return something like: min,loc = [0, -6; 4, 11]










share|improve this question























  • So if you know how to find peaks in a vector x, to find the minima just find the peaks of -x.

    – obchardon
    Nov 14 '18 at 14:09














1












1








1








For some "big data" processing. When plotted my data is sign wave like but with random peaks. (So imagine plotting the value of each matrix position against it's position)



I know how to find the peaks, but I need a way of then finding the value of local minima either side of the peaks and the position in the matrix. For example, if the data were:



3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0



The function I need would return something like: min,loc = [0, -6; 4, 11]










share|improve this question














For some "big data" processing. When plotted my data is sign wave like but with random peaks. (So imagine plotting the value of each matrix position against it's position)



I know how to find the peaks, but I need a way of then finding the value of local minima either side of the peaks and the position in the matrix. For example, if the data were:



3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0



The function I need would return something like: min,loc = [0, -6; 4, 11]







matlab max min minima






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 13:49









user2587726user2587726

2327




2327













  • So if you know how to find peaks in a vector x, to find the minima just find the peaks of -x.

    – obchardon
    Nov 14 '18 at 14:09



















  • So if you know how to find peaks in a vector x, to find the minima just find the peaks of -x.

    – obchardon
    Nov 14 '18 at 14:09

















So if you know how to find peaks in a vector x, to find the minima just find the peaks of -x.

– obchardon
Nov 14 '18 at 14:09





So if you know how to find peaks in a vector x, to find the minima just find the peaks of -x.

– obchardon
Nov 14 '18 at 14:09












2 Answers
2






active

oldest

votes


















2














If you have access to R2017b or later, check out the islocalmax and islocalmin functions.






share|improve this answer
























  • I'm stuck as a 2017a pleb right now :/

    – user2587726
    Nov 15 '18 at 10:40



















1














MATLAB R2007a and newer have a function called findpeaks (which requires the Signal Processing Toolbox). The syntax that you're looking for is



[pks,locs] = findpeaks(data)


Specifically,



>> [pks,locs] = findpeaks(-[3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0]) % note it's "-[data]"

pks =
0 6
locs =
4 11


The minus is because we want the "valleys" and not "peaks", so make sure you don't forget to negate pks afterwards.






share|improve this answer
























  • Whilst findpeaks works for this example, my actual data is much longer and this doesn't work I'm afraid (My apologies for not making this clear in my original question). Imagine the data were: A = [-9 3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0 -9]; Then findpeaks(-A) would find the -9's, not the 0 and -6.

    – user2587726
    Nov 15 '18 at 10:23











  • @user2587726 you can configure the "prominence" of the extrema that findpeaks returns. See here. Also, you can use sign(diff(data)), then look for [-1 1] in the resulting vector.

    – Dev-iL
    Nov 15 '18 at 16:01











  • I'm confused about why you say findpeaks wouldn't find the 0 and -6. By default, it will (actually in the example you gave it won't find the -9 because they are on the ends). When I try this, I get peaks of 0 and 6 at locations 5 and 12 (which you'd then negate). If you also want the endpoints, just add -inf to both ends of the input.

    – CKT
    Nov 15 '18 at 17:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301780%2ffinding-local-minima-about-either-side-of-a-maxima-in-a-matrix-in-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If you have access to R2017b or later, check out the islocalmax and islocalmin functions.






share|improve this answer
























  • I'm stuck as a 2017a pleb right now :/

    – user2587726
    Nov 15 '18 at 10:40
















2














If you have access to R2017b or later, check out the islocalmax and islocalmin functions.






share|improve this answer
























  • I'm stuck as a 2017a pleb right now :/

    – user2587726
    Nov 15 '18 at 10:40














2












2








2







If you have access to R2017b or later, check out the islocalmax and islocalmin functions.






share|improve this answer













If you have access to R2017b or later, check out the islocalmax and islocalmin functions.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 14:27









CKTCKT

72645




72645













  • I'm stuck as a 2017a pleb right now :/

    – user2587726
    Nov 15 '18 at 10:40



















  • I'm stuck as a 2017a pleb right now :/

    – user2587726
    Nov 15 '18 at 10:40

















I'm stuck as a 2017a pleb right now :/

– user2587726
Nov 15 '18 at 10:40





I'm stuck as a 2017a pleb right now :/

– user2587726
Nov 15 '18 at 10:40













1














MATLAB R2007a and newer have a function called findpeaks (which requires the Signal Processing Toolbox). The syntax that you're looking for is



[pks,locs] = findpeaks(data)


Specifically,



>> [pks,locs] = findpeaks(-[3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0]) % note it's "-[data]"

pks =
0 6
locs =
4 11


The minus is because we want the "valleys" and not "peaks", so make sure you don't forget to negate pks afterwards.






share|improve this answer
























  • Whilst findpeaks works for this example, my actual data is much longer and this doesn't work I'm afraid (My apologies for not making this clear in my original question). Imagine the data were: A = [-9 3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0 -9]; Then findpeaks(-A) would find the -9's, not the 0 and -6.

    – user2587726
    Nov 15 '18 at 10:23











  • @user2587726 you can configure the "prominence" of the extrema that findpeaks returns. See here. Also, you can use sign(diff(data)), then look for [-1 1] in the resulting vector.

    – Dev-iL
    Nov 15 '18 at 16:01











  • I'm confused about why you say findpeaks wouldn't find the 0 and -6. By default, it will (actually in the example you gave it won't find the -9 because they are on the ends). When I try this, I get peaks of 0 and 6 at locations 5 and 12 (which you'd then negate). If you also want the endpoints, just add -inf to both ends of the input.

    – CKT
    Nov 15 '18 at 17:53
















1














MATLAB R2007a and newer have a function called findpeaks (which requires the Signal Processing Toolbox). The syntax that you're looking for is



[pks,locs] = findpeaks(data)


Specifically,



>> [pks,locs] = findpeaks(-[3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0]) % note it's "-[data]"

pks =
0 6
locs =
4 11


The minus is because we want the "valleys" and not "peaks", so make sure you don't forget to negate pks afterwards.






share|improve this answer
























  • Whilst findpeaks works for this example, my actual data is much longer and this doesn't work I'm afraid (My apologies for not making this clear in my original question). Imagine the data were: A = [-9 3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0 -9]; Then findpeaks(-A) would find the -9's, not the 0 and -6.

    – user2587726
    Nov 15 '18 at 10:23











  • @user2587726 you can configure the "prominence" of the extrema that findpeaks returns. See here. Also, you can use sign(diff(data)), then look for [-1 1] in the resulting vector.

    – Dev-iL
    Nov 15 '18 at 16:01











  • I'm confused about why you say findpeaks wouldn't find the 0 and -6. By default, it will (actually in the example you gave it won't find the -9 because they are on the ends). When I try this, I get peaks of 0 and 6 at locations 5 and 12 (which you'd then negate). If you also want the endpoints, just add -inf to both ends of the input.

    – CKT
    Nov 15 '18 at 17:53














1












1








1







MATLAB R2007a and newer have a function called findpeaks (which requires the Signal Processing Toolbox). The syntax that you're looking for is



[pks,locs] = findpeaks(data)


Specifically,



>> [pks,locs] = findpeaks(-[3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0]) % note it's "-[data]"

pks =
0 6
locs =
4 11


The minus is because we want the "valleys" and not "peaks", so make sure you don't forget to negate pks afterwards.






share|improve this answer













MATLAB R2007a and newer have a function called findpeaks (which requires the Signal Processing Toolbox). The syntax that you're looking for is



[pks,locs] = findpeaks(data)


Specifically,



>> [pks,locs] = findpeaks(-[3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0]) % note it's "-[data]"

pks =
0 6
locs =
4 11


The minus is because we want the "valleys" and not "peaks", so make sure you don't forget to negate pks afterwards.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 16:18









Dev-iLDev-iL

17.2k64377




17.2k64377













  • Whilst findpeaks works for this example, my actual data is much longer and this doesn't work I'm afraid (My apologies for not making this clear in my original question). Imagine the data were: A = [-9 3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0 -9]; Then findpeaks(-A) would find the -9's, not the 0 and -6.

    – user2587726
    Nov 15 '18 at 10:23











  • @user2587726 you can configure the "prominence" of the extrema that findpeaks returns. See here. Also, you can use sign(diff(data)), then look for [-1 1] in the resulting vector.

    – Dev-iL
    Nov 15 '18 at 16:01











  • I'm confused about why you say findpeaks wouldn't find the 0 and -6. By default, it will (actually in the example you gave it won't find the -9 because they are on the ends). When I try this, I get peaks of 0 and 6 at locations 5 and 12 (which you'd then negate). If you also want the endpoints, just add -inf to both ends of the input.

    – CKT
    Nov 15 '18 at 17:53



















  • Whilst findpeaks works for this example, my actual data is much longer and this doesn't work I'm afraid (My apologies for not making this clear in my original question). Imagine the data were: A = [-9 3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0 -9]; Then findpeaks(-A) would find the -9's, not the 0 and -6.

    – user2587726
    Nov 15 '18 at 10:23











  • @user2587726 you can configure the "prominence" of the extrema that findpeaks returns. See here. Also, you can use sign(diff(data)), then look for [-1 1] in the resulting vector.

    – Dev-iL
    Nov 15 '18 at 16:01











  • I'm confused about why you say findpeaks wouldn't find the 0 and -6. By default, it will (actually in the example you gave it won't find the -9 because they are on the ends). When I try this, I get peaks of 0 and 6 at locations 5 and 12 (which you'd then negate). If you also want the endpoints, just add -inf to both ends of the input.

    – CKT
    Nov 15 '18 at 17:53

















Whilst findpeaks works for this example, my actual data is much longer and this doesn't work I'm afraid (My apologies for not making this clear in my original question). Imagine the data were: A = [-9 3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0 -9]; Then findpeaks(-A) would find the -9's, not the 0 and -6.

– user2587726
Nov 15 '18 at 10:23





Whilst findpeaks works for this example, my actual data is much longer and this doesn't work I'm afraid (My apologies for not making this clear in my original question). Imagine the data were: A = [-9 3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0 -9]; Then findpeaks(-A) would find the -9's, not the 0 and -6.

– user2587726
Nov 15 '18 at 10:23













@user2587726 you can configure the "prominence" of the extrema that findpeaks returns. See here. Also, you can use sign(diff(data)), then look for [-1 1] in the resulting vector.

– Dev-iL
Nov 15 '18 at 16:01





@user2587726 you can configure the "prominence" of the extrema that findpeaks returns. See here. Also, you can use sign(diff(data)), then look for [-1 1] in the resulting vector.

– Dev-iL
Nov 15 '18 at 16:01













I'm confused about why you say findpeaks wouldn't find the 0 and -6. By default, it will (actually in the example you gave it won't find the -9 because they are on the ends). When I try this, I get peaks of 0 and 6 at locations 5 and 12 (which you'd then negate). If you also want the endpoints, just add -inf to both ends of the input.

– CKT
Nov 15 '18 at 17:53





I'm confused about why you say findpeaks wouldn't find the 0 and -6. By default, it will (actually in the example you gave it won't find the -9 because they are on the ends). When I try this, I get peaks of 0 and 6 at locations 5 and 12 (which you'd then negate). If you also want the endpoints, just add -inf to both ends of the input.

– CKT
Nov 15 '18 at 17:53


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301780%2ffinding-local-minima-about-either-side-of-a-maxima-in-a-matrix-in-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values