Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the maximum value of a matrix subset in MATLAB while preserving the indices of the full matrix

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!

like image 359
yy08 Avatar asked Jan 28 '11 21:01

yy08


People also ask

How do you find the maximum value of indices of a matrix in MATLAB?

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.

How do you find the maxima in MATLAB?

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.

How do you find the minimum value of an index in MATLAB?

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.

How do you find the maximum and minimum of a matrix?

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.


1 Answers

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);
like image 179
aschepler Avatar answered Nov 15 '22 05:11

aschepler