finding indexed max values of a matrix using logical array
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))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)) 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)) indexing, logical arrays MATLAB Answers — New Questions