Find n minimum values in an array - matlab

I am using Matlab 2012a.
I have an array of k cells (say 1000). I need to find the 5 lowest values of this array and need to do an average of those values in X and Y.
Anyone has an idea how to do that?

Assuming you have arrays X and Y, and you want to find the five lowest Y values:
[m mi] = sort(Y);
lowest5index = mi(1:5);
lowest5Y = Y(lowest5index);
lowest5X = X(lowest5index);
meanYlowest5 = mean(lowest5Y);
meanXlowest5 = mean(lowest5X);
Explanation:
The sort command with two output parameters returns both the sorted array (in m) and the indices in the original array (mi). The first five indices mi(1:5) correspond to the five lowest values. Taking the mean of these values for both X and Y will do what we want. If I didn't understand your problem statement, please clarify your question and I will take another shot at it.

How about doing a sort of your array from lowest value to the highest and then selecting the 5 first values. Those will be the 5 min values of your array. Then perform a mean of those 5 values.
This might not be the most memory efficient way of doing this but for just 1000 values it will get the job done!
Hope it helps!

use minmaxselection MATLAB MEX package, which has been specially optimized for this problem:
a = [2,3,4,7,56,4,21, 64, -2];
mink(a, 2)
<< ans =
<< -2 2
mink(a,4)
<< ans =
<< -2 2 3 4

Related

What does it mean to use logical indexing/masking to extract data from a matrix? (MATLAB)

I am new to matlab and I was wondering what it meant to use logical indexing/masking to extract data from a matrix.
I am trying to write a function that accepts a matrix and a user-inputted value to compute and display the total number of values in column 2 of the matrix that match with the user input.
The function itself should have no return value and will be called on later in another loop.
But besides all that hubbub, someone suggested that I use logical indexing/masking in this situation but never told me exactly what it was or how I could use it in my particular situation.
EDIT: since you updated the question, I am updating this answer a little.
Logical indexing is explained really well in this and this. In general, I doubt, if I can do a better job, given available time. However, I would try to connect your problem and logical indexing.
Lets declare an array A which has 2 columns. First column is index (as 1,2,3,...) and second column is its corresponding value, a random number.
A(:,1)=1:10;
A(:,2)=randi(5,[10 1]); //declares a 10x1 array and puts it into second column of A
userInputtedValue=3; //self-explanatory
You want to check what values in second column of A are equal to 3. Imagine as if you are making a query and MATLAB is giving you binary response, YES (1) or NO (0).
q=A(:,2)==3 //the query, what values in second column of A equal 3?
Now, for the indices where answer is YES, you want to extract the numbers in the first column of A. Then do some processing.
values=A(q,2); //only those elements will be extracted: 1. which lie in the
//second column of A AND where q takes value 1.
Now, if you want to count total number of values, just do:
numValues=length(values);
I hope now logical indexing is clear to you. However, do read the Mathworks posts which I have mentioned earlier.
I over simplified the code, and wrote more code than required in order to explain things. It can be achieved in a single-liner:
sum(mat(:,2)==userInputtedValue)
I'll give you an example that may illustrate what logical indexing is about:
array = [1 2 3 0 4 2];
array > 2
ans: [0 0 1 0 1 0]
using logical indexing you could filter elements that fullfil a certain condition
array(array>2) will give: [3 4]
you could also perform alterations to only those elements:
array(array>2) = 100;
array(array<=2) = 0;
will result in "array" equal to
[0 0 100 0 100 0]
Logical indexing means to have a logical / Boolean matrix that is the same size as the matrix that you are considering. You would use this as input into the matrix you're considering, and any locations that are true would be part of the output. Any locations that are false are not part of the output. To perform logical indexing, you would need to use logical / Boolean operators or conditions to facilitate the selection of elements in your matrix.
Let's concentrate on vectors as it's the easiest to deal with. Let's say we had the following vector:
>> A = 1:9
A =
1 2 3 4 5 6 7 8 9
Let's say I wanted to retrieve all values that are 5 or more. The logical condition for this would be A >= 5. We want to retrieve all values in A that are greater than or equal to 5. Therefore, if we did A >= 5, we get a logical vector which tells us which values in A satisfy the above condition:
>> A >= 5
ans =
0 0 0 0 1 1 1 1 1
This certainly tells us where in A the condition is satisfied. The last step would be to use this as input into A:
>> B = A(A >= 5)
B =
5 6 7 8 9
Cool! As you can see, there isn't a need for a for loop to help us select out elements that satisfy a condition. Let's go a step further. What if I want to find all even values of A? This would mean that if we divide by 2, the remainder would be zero, or mod(A,2) == 0. Let's extract out those elements:
>> C = A(mod(A,2) == 0)
C =
2 4 6 8
Nice! So let's go back to your question. Given your matrix A, let's extract out column 2.
>> col = A(:,2)
Now, we want to check to see if any of column #2 is equal to a certain value. Well we can generate a logical indexing array for that. Let's try with the value of 3:
>> ind = col == 3;
Now you'll have a logical vector that tells you which locations are equal to 3. If you want to determine how many are equal to 3, you just have to sum up the values:
>> s = sum(ind);
That's it! s contains how many values were equal to 3. Now, if you wanted to write a function that only displayed how many values were equal to some user defined input and displayed this event, you can do something like this:
function checkVal(A, val)
disp(sum(A(:,2) == val));
end
Quite simply, we extract the second column of A and see how many values are equal to val. This produces a logical array, and we simply sum up how many 1s there are. This would give you the total number of elements that are equal to val.
Troy Haskin pointed you to a very nice link that talks about logical indexing in more detail: http://www.mathworks.com/help/matlab/math/matrix-indexing.html?refresh=true#bq7eg38. Read that for more details on how to master logical indexing.
Good luck!
%% M is your Matrix
M = randi(10,4)
%% Val is the value that you are seeking to find
Val = 6
%% Col is the value of the matrix column that you wish to find it in
Col = 2
%% r is a vector that has zeros in all positions except when the Matrix value equals the user input it equals 1
r = M(:,Col)==Val
%% We can now sum all the non-zero values in r to get the number of matches
n = sum(r)
M =
4 2 2 5
3 6 7 1
4 4 1 6
5 8 7 8
Val =
6
Col =
2
r =
0
1
0
0
n =
1

How can I find the indices of the 2 smallest elements in a vector without sorting?

I'm trying to find the two smallest elements of a 1xn vector. The catch is that I can't sort it because the indices are linearly dependent upon the values (so sorting the values will screw up to original indices) AND 0 can be one of the elements. Also, elements can repeat. Here's a simplified example of my code:
a = [1,5,8,7,1];
find(a==min(a))
ans =
1 5
For a, this is the answer I was expecting.
b = [0,8,6,1,9];
find(b==min(b))
ans =
1
For b, I need it to find the 0 and the 1 so it should give me back 1 and 4 respectively for the indices. Thanks in advance!
Phil Goddard's answer is perfectly acceptable. However, you did say that you want to do this without sorting, so I'm assuming you don't want to use the sort function at all. What you can do is use min twice. Once you invoke it the first time and you find the index of the minimum element, you would set this location in your array to NaN, then run min an additional time. By setting the location to NaN, you would effectively skip the element that is equal to the smallest at that point in time. After you call min the second time, you'll get the second smallest element.
One small thing you'll need to do afterwards is to clear off the NaN you set in the array after the first min call. You do this by extracting what the minimum value was after the first call, in addition to where this minimum value was located. Once you call min a second time, you'd reset the location of where the first minimum was from NaN back to its original value.
In other words:
a = [1,5,8,7,1];
[min1,ind1] = min(a);
a(ind1) = NaN;
[~,ind2] = min(a);
a(ind1) = min1; %// Copy back to ensure we get original data back
ind1 and ind2 will contain the locations of the two smallest values in a. With your example, I get:
disp([ind1 ind2])
1 5
Similarly, for b, this is what we get with the above code:
disp([ind1 ind2])
1 4
You should use the second output from sort,
>> [~,idx] = sort(a);
>> idx(1:2)
ans =
1 5
>> [~,idx] = sort(b);
>> idx(1:2)
ans =
1 4

Remove values from a vector based on restriction

If i have a vector (array) in matlab, is it possible to remove the values from that vector based on some restriction (e.g.. all non negative numbers).
Can you please advise me on the best approach to do that.
Yes, you can either use logical indexing to keep the values which meet a criterion or use the find function to get the indexes which hold values that meet a criterion.
logical indexing
the find function
An example of logical indexing where we want to remove all the values from a vector which are not greater than three:
>> x=[1,2,3,4,5,6]
x =
1 2 3 4 5 6
>> x=x(x>3)
x =
4 5 6
You can also ask for multiple criteria as you would expect. In the following example, we want to keep every value which is greater than three, but not five.
>> x=[1,2,3,4,5,6]
x =
1 2 3 4 5 6
>> x=x(x>3 & x~=5)
x =
4 6
Finally, the find function can come in handy when you need the indexes of values which meet a criterion.
>> x=[1,1,2,2,5,5]
x =
1 1 2 2 5 5
>> ind=find(x>3)
ind =
5 6
Logical indexing and find can also be applied to matrices with more than one row/column.
Thanks #Alan for helping me improve the answer.
You may want to look into logical indexing, as it neatly handles your problem.
To use the example you gave, if you have a vector a of numbers, and you want to remove all negative numbers you could do the following:
b = a(a >= 0);
which would create a vector b containing only the positive elements of a, or you could try:
a(a < 0) = [];
would set any elements in the vector a to []

frequency of each vector value in another vector matlab

I need to calculate the frequency of each value in another vector in MATLAB.
I can use something like
for i=1:length(pdata)
gt(i)=length(find(pf_test(:,1)==pdata(i,1)));
end
But I prefer not to use loop because my dataset is quite large. Is there anything like histc (which is used to find the frequency of values in one vector) to find the frequency of one vector value in another vector?
If your values are only integers, you could do the following:
range = min(pf_test):max(pf_test);
count = histc(pf_test,range);
gt = count(ismember(range,a));
gt(~ismember(unique(a),b)) = 0;
If you can't guarantee that the values are integers, it's a bit more complicated. One possible method of it would be the following:
%restrict yourself to values that appear in the second vector
filter = ismember(pf_test,pdata);
% sort your first vector (ignore this if it is already sorted)
spf_test = sort(pf_test);
% Find the first and last occurrence of each element
[~,last] = unique(spf_test(filter));
[~,first] = unique(spf_test(filter),'first');
% Initialise gt
gt = zeros(length(pf_test));
% Fill gt
gt(filter) = (last-first)+1;
EDIT: Note that I may have got the vectors the wrong way around - if this doesn't work as expected, switch pf_test and pdata. It wasn't immediately clear to me which was which.
You mention histc. Why are you not using it (in its version with two input parameters)?
>> pdata = [1 1 3 2 3 1 4 4 5];
>> pf_test = 1:6;
>> histc(pdata,pf_test)
ans =
3 1 2 2 1 0

How to get MATLAB to display the index of the minimum value in a 2D array?

I'm trying to write a script in MATLAB that finds the location of the minimum value of a 2D array of numbers. I am certain there is only 1 minimum in this array, so having multiple locations in the array with the same minimum value is not an issue. I can find the minimum value of the array, but in a 30x30 array, I would like to know which row and column that minimum value is in.
As an alternative version, combine min to get the minimum value and find to return the index, if you've already calculated the minimum then just use find.
>> a=magic(30);
>> [r,c]=find(a==min(min(a)))
r =
1
c =
8
Or depending on how you want to use the location information you may want to define it with a logical array instead, in which case logical addressing can be used to give you a truth table.
>> a=magic(30);
>> locn=(a==min(min(a)));
You could reshape the matrix to a vector, find the index of the minimum using MIN and then convert this linear index into a matrix index:
>> x = randi(5, 5)
x =
5 4 4 2 4
4 2 4 5 5
3 1 3 4 3
3 4 2 5 1
2 4 5 3 5
>> [value, index] = min(reshape(x, numel(x), 1));
>> [i,j] = ind2sub(size(x), index)
i =
3
j =
2
Look at the description of the min function. It can return the minimum value as well as the index. For a two dimensional array, just call it twice.
A = rand(30); % some matrix
[minColVal, minColIdx] = min(A);
[minRowVal, minRowIdx] = min(minColVal);
minVal = minRowVal;
minValIdx = [minColIdx(minRowIdx), minRowIdx];
Edit: #b3's solution is probably computationally more elegant (faster and needs less temporary space)
To find min or max in a subset of a vector -
If A is a vector and "lowerBound" and "upperBound" are the bounds of the vector among which you need to find the max (or min) value, then use this command -
[Value,Index]=min(A(lowerBound:upperBound));
This returns "Value" as the min or max value among A(lowerBound) and A(uppedBound) and
"Index" as with "lowerBound" as the offset. So to find the absolute index, you need to add "lowerBound" to the Index.
An alternate solution using an inline function will work.
>> min_index = #(matrix) find(matrix == min(reshape(matrix, [1,numel(matrix)])));
>> a=magic(30);
>> [r,c]=min_index(a)
r =
1
c =
8