using Matlab, how to find the maximum value over a certain range? - matlab

i want to know the coding to find the maximum value, over a certain range.
i already coded like below.
f=f'
ac_yyyy_f=ac_yyyy_f'
[row,col] = ind2sub(size(ac_yyyy_f),find(ac_yyyy_f==max(ac_yyyy_f)))
but the problem is, sometimes the maximum value of Y axis choosen by my code is not what i want.
the X axis has the range of 0 to 100000 and i want the maximum between 20000 to 100000. the problem is sometimes the max value show up at the range of 0 to 20000.
How can i figure this out?

Use the max() function:
%let R be your range of values
R = [2 1 7 4];
[value, index] = max(R);
In the above example, value will be 7 and index will be 3
For more info: http://fr.mathworks.com/help/matlab/ref/max.html

I'm using a vector of random integers to stand in for your function output.
a = floor(rand(1,100000)*100);
[val, idx] = max(a(20000:100000));
You want to use the max function here to find the maximum value rather than find.
Now, the other part of the task is getting the max value from a certain part of your matrix. You can pass just a subset of a vector or matrix to a function by indexing it with a range of values. Note that idx gives you the position of val within a(20000:100000). If you need the position within a, you need to use idx+19999.
Also, you should take a look at the matrix indexing reference—there are many different and fun ways to index a matrix—because indexing is one of the most important features of matlab.
Here's the reference for the max function:
http://www.mathworks.com/help/matlab/ref/max.html
And the reference for indexing:
http://www.mathworks.com/help/matlab/math/matrix-indexing.html

You can use the max function with a subset of your array. It will return the maximum value, as well as the index where it is located. Be sure to correct the index it returns you based on your desired range. Like this:
%//create an array of 100,000 values to play with.
f=floor(rand(100000,1).*100);
%//find the max between f(20000) and f(100000)
[myMax, I] = max( f(20000:100000) );
%//correct the index based on where we started looking
%//for the max. Subtract 1 because it's MATLAB!
myIndex = I+20000-1;
This results in:
>> myMax
myMax =
99
>> myIndex
myIndex =
20045
>> f(myIndex)
ans =
99

Related

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

How can I find the maximum value and its index in array in MATLAB?

Suppose I have an array, a = [2 5 4 7]. What is the function returning the maximum value and its index?
For example, in my case that function should return 7 as the maximum value and 4 as the index.
The function is max. To obtain the first maximum value you should do
[val, idx] = max(a);
val is the maximum value and idx is its index.
For a matrix you can use this:
[M,I] = max(A(:))
I is the index of A(:) containing the largest element.
Now, use the ind2sub function to extract the row and column indices of A corresponding to the largest element.
[I_row, I_col] = ind2sub(size(A),I)
source: https://www.mathworks.com/help/matlab/ref/max.html
In case of a 2D array (matrix), you can use:
[val, idx] = max(A, [], 2);
The idx part will contain the column number of containing the max element of each row.
You can use max() to get the max value. The max function can also return the index of the maximum value in the vector. To get this, assign the result of the call to max to a two element vector instead of just a single variable.
e.g.
z is your array,
>> [x, y] = max(z)
x =
7
y =
4
Here, 7 is the largest number at the 4th position(index).
3D case
Modifying Mohsen's answer for 3D array:
[M,I] = max (A(:));
[ind1, ind2, ind3] = ind2sub(size(A),I)
This will return the maximum value in a matrix
max(M1(:))
This will return the row and the column of that value
[x,y]=ind2sub(size(M1),max(M1(:)))
For minimum just swap the word max with min and that's all.
For example:
max_a = max(a)
a.index(max_a)

Find upper bound index in sorted vector

I have a vector like the following in Matlab.
Columns 1 through 4
0.160000000000000 0.208000000000000 0.244000000000000 0.268000000000000
Column 5
0.280000000000000
And I want to find the upper bound index of the following value:
0.16230400000000
I know this value is between the 1° and 2° indices, but I want to find the upper bound which in this case is index 2, even when the closest value is at index 1. How do I get that in Matlab without a loop?
I found the following method to find the closest value but I need always the upper bound.
[c ind] = min(abs(probCum-codComp));
You can get the index of the upper bound by counting how many entries are smaller than the value of interest increased by one:
id = sum(value < data)+1;
Note that, if all elements are smaller than the value of interest, data(id) will return an error because id is larger than the size of data.
If your array is sorted in an ascending fashion, the find command is especially powerful:
find (a>0.162,1)
You can subtract the value you search for from the vector and observe the sign
V = [0.1600 0.2080 0.2440 0.2680 0.2800];
v = 0.162304;
V-v
ans =
-0.0023 0.0457 0.0817 0.1057 0.1177
Now you can find the first non-negative entry in this vector - this is your upper bound
find(V-v>=0, 1, 'first')
ans =
2
You can also change the inequality in the expression above to suite your needs.
You can use the second output from min to get the indices to the "proper" index:
[~,index] = min(abs(data-value));
then check if the lower or upper was taken, and adjust index accordingly:
if value > data(index)
index = min(length(data), index+1); end

Find highest/lowest value in matrix

very basic question: How can I find the highest or lowest value in a random matrix.
I know there is a possibility to say:
a = find(A>0.5)
but what I'm looking for would be more like this:
A = rand(5,5)
A =
0.9388 0.9498 0.6059 0.7447 0.2835
0.6338 0.0104 0.5179 0.8738 0.0586
0.9297 0.1678 0.9429 0.9641 0.8210
0.0629 0.7553 0.7412 0.9819 0.1795
0.3069 0.8338 0.7011 0.9186 0.0349
% find highest (or lowest) value
ans = A(19)%for the highest or A(7) %for the lowest value in this case
Have a look at the min() and max() functions. They can return both the highest/lowest value, and its index:
[B,I]=min(A(:)); %# note I fixed a bug on this line!
returns I=7 and B=A(7)=A(2,2). The expression A(:) tells MATLAB to treat A as a 1D array for now, so even though A is 5x5, it returns the linear index 7.
If you need the 2D coordinates, i.e. the "2,2" in B=A(7)=A(2,2), you can use [I,J] = ind2sub(size(A),I) which returns I=2,J=2, see here.
Update
If you need all the entries' indices which reach the minimum value, you can use find:
I = find(A==min(A(:));
I is now a vector of all of them.
For matrices you need to run the MIN and MAX functions twice since they operate column-wise, i.e. max(A) returns a vector with each element being the maximum element in the corresponding column of A.
>> A = rand(4)
A =
0.421761282626275 0.655740699156587 0.678735154857773 0.655477890177557
0.915735525189067 0.0357116785741896 0.757740130578333 0.171186687811562
0.792207329559554 0.849129305868777 0.743132468124916 0.706046088019609
0.959492426392903 0.933993247757551 0.392227019534168 0.0318328463774207
>> max(max(A))
ans =
0.959492426392903
>> min(min(A))
ans =
0.0318328463774207
Note that this only works for matrices. Higher dimensional arrays would require running MIN and MAX as many times as there are dimensions which you can get using NDIMS.
Try this out
A=magic(5)
[x,y]=find(A==max(max(A))) %index maximum of the matrix A
A_max=A(x,y)
[x1,y1]=find(A==min(max(A))) %index minimum of the matrix A
A_min=A(x1,y1)

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