Clear Filters
Clear Filters

finding indexed max values of a matrix using logical array

7 views (last 30 days)
Hi. Note I have a hard-coded example for this question below you can run to see what I am referring in the question.
To illustrate the problem, I have an m x n matrix G of values. Each row represents a time series of a given repeated measure. I want to get the max values along each row of G and find the (i,j) indexes of the max values of G. Then I want to find the exact time point for where in the time series the max value of G lie for each corresponding row.
My approach was to first make the time series S a repeated matix of the same row values. Then I made a logical mask M = (G == max(G, [],2)). The occurrences of logical ones do correspond to where the max values of G lie, as expected. However when I used the the logical mask M in S to get the time points for the max values of each row using S(M), I get nonsense. I am not sure what I am not understanding or doing wrong.
For instance. The first row max of G is 3 and at index =3 in the time series. Using the S(M) scheme should give "202" but doesn't. The values for the other rows are all off as well.
Any help with this would be greatly appreciated.
_______________________________________________
UPDATE:
I realized that the scheme is technically working with the problem the values for the sequential rows is not preserved. When I type:
[i,j] = find(G == max(G, [],2)), the row order is weirdly randomized (see code section below of the order of the i values). Does any one know why this would happen? I would need to preserve the sequential order of the row values from 1:9.
S = repmat((200:202), [9,1]); % repeated matrix of consecutive indices
G = [2 1 3
4 8 12
3 4 2
2 1 3
4 8 12
3 4 2
2 1 3
4 8 12
3 4 2]; % matrix of random values
M = (G == max(G, [],2)) % find logical mask of max values along the rows
M = 9x3 logical array
0 0 1 0 0 1 0 1 0 0 0 1 0 0 1 0 1 0 0 0 1 0 0 1 0 1 0
S(M)
ans = 9x1
201 201 201 202 202 202 202 202 202
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[i,j] = find(G == max(G, [],2))
i = 9x1
3 6 9 1 2 4 5 7 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
j = 9x1
2 2 2 3 3 3 3 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Accepted Answer

SACHIN KHANDELWAL
SACHIN KHANDELWAL on 29 May 2024
Hi @hxen,
The issue you are encountering with the random ordering of row indices i when using [i,j] = find(G==max(G,[],2) is due to how "find" function works.
The "find" function scans the matrix in column by coumn and returns indices in the order it encounters them.
To preserve the sequential order of the row values from 1 to 9, you can use a different approach. Instead of using find, you can directly work with the logical matrix M to obtain the row and column indices of the max values, ensuring they are in the order of the rows.
Here's how you can modify your approach:
  1. First, figure out which column has the highest value for each row.
  2. Use the "sub2ind" function to turn those row and column numbers into positions in the matrix.
  3. Then, get the values from those positions.
G = [2 1 3
4 8 12
3 4 2
2 1 3
4 8 12
3 4 2
2 1 3
4 8 12
3 4 2];
S = repmat((200:202), [9,1]);
[~, j] = max(G, [], 2);
% here j is column wise index
% i is row index
i = (1:size(G,1))';
% calculate the linear indices
linearIndices = sub2ind(size(S), i, j);
% find the value based on linear Indices
S(linearIndices)
Hope it helps!
  1 Comment
hxen
hxen on 29 May 2024
Indeed it does help! Thank you for your post and also introducing me to the sub2ind function! Much appreciated! :)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!