Nischal
Nischal

Reputation: 67

Matlab multiple minimum values

I am writing a program in MATLAB, in which i generate a 1-D array(14 elements).. The elements of an array can take 5 distinct values... I want to find out what is the minimum of the array.. and ALSO if there is a unique minimum or if there are more than one minimum...

What is the most efficient way to do this.. for finding the minimum i can use the min function in MATLAB.. how to find if there are multiple instances of that minimum.. Note, i want to iterate this process a Huge no. of times(~10000) and tabulate the number of times each case occurs..

Upvotes: 3

Views: 5377

Answers (1)

Jonas
Jonas

Reputation: 74940

For a single 1D array, the two values can be found like this:

minValue = min(myArray);
numberOfMinValues = sum(myArray==minValue);

If your 1D arrays are always of the same length (and you have enough RAM), you can catenate them into a single large array, after which you only apply the functions once to save time:

%# assuming each 1D array is a column vector (N-by-1)
%# minValues is 1-by-M, i.e. the minimum of each of the M 1D arrays
minValues = min(myLargeArray,[],1); 
%# numberOfMinValues is, again, 1-by-M
numberOfMinValues = sum(bsxfun(@eq,myLargeArray,minValues),1);

Upvotes: 4

Related Questions