Currently, I'm able to find the max value of a matrix C
and its index with the following code:
[max_C, imax] = max(C(:));
[ypeak, xpeak] = ind2sub(size(C),imax(1));
Let's call a subset of the matrix C_sub
I want to find the max value of C_sub
, but I also want to know the index of that max value in C
.
Seems like it should be an easy problem, but it has me stumped.
Thanks for your help!
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.
Description. pks = findpeaks( data ) returns a vector with the local maxima (peaks) of the input signal vector, data . A local peak is a data sample that is either larger than its two neighboring samples or is equal to Inf . The peaks are output in order of occurrence.
M = min( A ,[], dim ) returns the minimum element along dimension dim . For example, if A is a matrix, then min(A,[],2) is a column vector containing the minimum value of each row.
Pair Comparison (Efficient method): Select two elements from the matrix one from the start of a row of the matrix another from the end of the same row of the matrix, compare them and next compare smaller of them to the minimum of the matrix and larger of them to the maximum of the matrix.
Suppose that C_sub
was created by
C_sub = C(rows,cols);
where rows
and cols
are vectors of indices. Save those rows
and cols
vectors somewhere you can reuse them, if you haven't already.
[max_C_sub, ind_C_sub] = max(C_sub(:));
[ypeak_sub, xpeak_sub] = ind2sub(size(C_sub), ind_C_sub);
xpeak = cols(xpeak_sub);
ypeak = rows(ypeak_sub);
Or if rows
and/or cols
was a vector of logicals instead of a vector of indices, you can convert them using find
, and then proceed as above.
rows_ind = find(rows_logical);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With