- First, figure out which column has the highest value for each row.
- Use the "sub2ind" function to turn those row and column numbers into positions in the matrix.
- Then, get the values from those positions.
finding indexed max values of a matrix using logical array
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
S(M)
[i,j] = find(G == max(G, [],2))
0 commentaires
Réponse acceptée
SACHIN KHANDELWAL
le 29 Mai 2024
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:
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!
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!