Prev: help in simscape
Next: regexprep tokens
From: Maya on 3 Jul 2010 05:13 Hey all, i would really appreciate some help in a rather simple problem (i think is is anyway). I don't seem to be able to find a proper function for detecting the indices of multiple maxima in a (1 dimensional) array. That is, it would be really nice if the [C,I]=max(A,[],dim) function had an additional argument for providing a number of how many of the largest values you want to find. Because it's not necessary that you need only the index of the largest one, but maybe of the 2 largest values (the one being smaller than the other, yet larger than the rest) etc... So, is there an easy way to do that? Thanks a lot! :)
From: us on 3 Jul 2010 07:56 "Maya " <fratz0la(a)hotmail.com> wrote in message <i0muv0$ram$1(a)fred.mathworks.com>... > Hey all, > i would really appreciate some help in a rather simple problem (i think is is anyway). I don't seem to be able to find a proper function for detecting the indices of multiple maxima in a (1 dimensional) array. That is, it would be really nice if the [C,I]=max(A,[],dim) function had an additional argument for providing a number of how many of the largest values you want to find. Because it's not necessary that you need only the index of the largest one, but maybe of the 2 largest values (the one being smaller than the other, yet larger than the rest) etc... > So, is there an easy way to do that? Thanks a lot! :) a hint: - look at one of the many peak finders on the FEX... us
From: Roger Stafford on 3 Jul 2010 13:52 "Maya " <fratz0la(a)hotmail.com> wrote in message <i0muv0$ram$1(a)fred.mathworks.com>... > Hey all, > i would really appreciate some help in a rather simple problem (i think is is anyway). I don't seem to be able to find a proper function for detecting the indices of multiple maxima in a (1 dimensional) array. That is, it would be really nice if the [C,I]=max(A,[],dim) function had an additional argument for providing a number of how many of the largest values you want to find. Because it's not necessary that you need only the index of the largest one, but maybe of the 2 largest values (the one being smaller than the other, yet larger than the rest) etc... > So, is there an easy way to do that? Thanks a lot! :) - - - - - - - - - - As has been mentioned here, sorting is one way. Suppose you want the k largest values in A. [B,ix] = sort(A); B = B(1:k); ix = ix(1:k); B and ix are the desired k largest values and the corresponding k indices, respectively. However, if k is small and A is long, this can be slower than need be. For example, if k is only 2, it really isn't necessary to sort the entire array just to find its two largest elements. One could do this: [b1,i1] = max(A); [b2,i2] = max(A([1:i1-1,i1+1:end])); if i2>=i1, i2 = i2+1; end B = [b1,b2]; % <-- The two largest elements of A ix = [i1,i2]; % <-- Their corresponding indices Roger Stafford
From: Roger Stafford on 3 Jul 2010 18:38 "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i0ntc5$egm$1(a)fred.mathworks.com>... > "Maya " <fratz0la(a)hotmail.com> wrote in message <i0muv0$ram$1(a)fred.mathworks.com>... > > ....... That is, it would be really nice if the [C,I]=max(A,[],dim) function had an additional argument for providing a number of how many of the largest values you want to find. ....... > - - - - - - - - - - > As has been mentioned here, sorting is one way. Suppose you want the k largest values in A. > ........ - - - - - - - - - If you have a serious interest in efficient algorithms for larger values of the k referred to above, you might read the Wikipedia article "Selection Algorithm" at: http://en.wikipedia.org/wiki/Selection_algorithm The section called "Selecting k smallest or largest elements" pertains to your problem and describes an algorithm that is only order n (for n elements in your A) if you do not need to have these k largest elements arranged in any particular order. It makes use of the "median of the medians" algorithm. As the article notes, "The resulting algorithm requires an expected time of only O(n), which is just about the best any algorithm can hope for." Roger Stafford
From: ImageAnalyst on 3 Jul 2010 20:26
Maya: You can find local maxima in 2D arrays using imregionalmax if you have the Image Processing Toolbox. It's like a peak finder. From the help: "imregionalmax Regional maxima Syntax BW = imregionalmax(I) BW = imregionalmax(I,conn) Description BW = imregionalmax(I) finds the regional maxima of I. imregionalmax returns the binary image BW that identifies the locations of the regional maxima in I. BW is the same size as I. In BW, pixels that are set to 1 identify regional maxima; all other pixels are set to 0. Regional maxima are connected components of pixels with a constant intensity value, and whose external boundary pixels all have a lower value. By default, imregionalmax uses 8-connected neighborhoods for 2-D images and 26-connected neighborhoods for 3-D images. For higher dimensions, imregionalmax uses conndef(ndims(I),'maximal'). BW = imregionalmax(I,conn) computes the regional maxima of I using the specified connectivity. conn can have any of the following scalar values." You could then call bwlabel on the array returned from imregionalmax to get a count of how many regional max it found. -Image Analyst |