This doesn't make any sense to me and I'm not sure what to even search for.
Matlab code:
[a b] = max(.9);
Output:
a =
0.9
b =
1
Why is it increasing by 1/10? What does [a b] do when on the left side of equal sign?
max is used to find the maximum value of an array. The second output (if requested), returns the index that corresponds to the first value in the array that is equal to the maximum value.
[max_value, max_index] = max([1 3 3 2]);
% max_value = 3
% max_index = 2
In your case, you are passing a scalar (a 1 x 1 array) to max, therefore the scalar is the maximum and the maximum appears at index 1.
[M,I] = max(A) finds the indices of the maximum values of A and returns them in output vector I, using any of the input arguments in the previous syntaxes. If the maximum value occurs more than once, then max returns the index corresponding to the first occurrence.
Reference: https://www.mathworks.com/help/matlab/ref/max.html?requestedDomain=www.mathworks.com
Related
I am trying with following code, but get indice of last value only
A=[ 3 4 1 2 4 4 4]
B=unique(A)
[b1 b2]=max(B)
while i<=numel(A)
if A(i)==A(b2)
ID=A(i)
end
end
Is there any way other in matlab to get all indices of value 4 (max value).
If you want to find the indices of the largest value in your matrix, there is no need for unique at all. It's superfluous. Just use find and max simultaneously:
ID = find(A == max(A));
max(A) returns the largest value in A. A == max(A) returns a logical vector where 1 corresponds to a value in A matching the largest value in A and 0 otherwise. Finally, find determines the locations in the input that are non-zero, so in effect we are finding the locations that match the largest value in A.
I have a Matlab time series data set, which consist of a signal that can only be 1 or 0. How can I get rid of all the values except for the changing ones?
For example:
1
1
1
0
1
0
0
0
should ideally result in
1
0
1
0
while keeping the correct time values as well of course.
Thing is, that I need to find the frequency of the signal. The time should be measured from 0->1 to the next time 0->1 occurs. The smallest time / highest frequency is what I need in the end.
Thanks!
You can use the getsamples method to get a time series which contains a subset of the original samples. Remains to identify the indices where the time series has changed, for this purpose you can use diff and logical indexing:
ts = timeseries([1 1 1 0 1 0 0 0],1:8)
ts.getsamples([true;squeeze(diff(ts.Data)) ~= 0])
A simple and clever call to to diff should be sufficient:
>> A = [1; 1; 1; 0; 1; 0; 0; 0];
>> B = A(diff([-Inf; A]) ~= 0)
B =
1
0
1
0
The code is quite simple. diff finds pairs of differences in an array. Concretely, given an array A, the output is of the following structure:
B = [A(2) - A(1), A(3) - A(2), ..., A(N) - A(N-1)];
N is the total length of the signal. This results in a N-1 length signal. As such, a trick that you can use is to append the array A with -Inf (or some high non-zero value) so that when you find the difference between the first element of this appended array and the actual first element of the true array, you will get some non-zero change. That is registered with diff([-Inf; A]). The next thing you'll want is to check is to see where the differences are non-zero. Whenever there is a non-zero difference, that is a position that you want to keep because there has been a change that occurred. This produces a logical array and so the last step is to use this to index into your array A and thus get the result.
This only extracts out the signal you need however. If you'd like to extract the time in between unique elements, supposing you had some time vector t that was as long as your signal stored in A. You would first record the logical vector in a separate variable, then index into both your time array and the signal array to extract out what you need (original idea from user dfri):
ind = diff([-Inf; A]) ~= 0;
times = t(ind);
B = A(ind);
You can make use of diff and logical to save the results as a logical array, used as a subsequent index filter in your data (say t for time and y for boolean values ))
%// example
t = 0:0.01:0.07;
y = [1,1,1,0,1,0,0,0];
%// find indices to keep
keep = [true logical(diff(y))];
%// truncated data
tTrunc = t(keep)
yTrunc = y(keep)
with the results for the example as follows
tTrunc =
0 0.0300 0.0400 0.0500
yTrunc =
1 0 1 0
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
I have a structure A with fields B and C.
Values of B are double and can take on the value of 1 or 2.
Values of C are double and range in value from 1 to about 50.
Both B and C have the same number of elements (when a value is assigned to C then a 1 or 2 is assigned to B).
The following code is supposed to find all index values where B equals 1 and then use those index values to find the minimum value of C.
>> a=find(A(1).B(:)==1);
>> [value,index]=min(A(1).C(a))
value = 5.020000000000000
index = 630
As you can see below, when I put the minimum value index back into B it returns a 2.
>> A(1).B(630)
ans = 2
The problem stems from the fact that vector a is not the same size as original vector A(1).B(:). So the value you get in index refers to the corresponding location in a not in A(1).C(:) to get the correct index use:
A(1).B(a(index))
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)