Extracting a vlue from a matrix using a neighboring value as a reference
1 view (last 30 days)
Show older comments
Alan Jurisich
on 25 Nov 2023
Commented: Dyuman Joshi
on 25 Nov 2023
How would I go about extracting an indivdual value from column based on the max value from anothe row?
Say I have the following 2 x 5:
1 2 4 10 3
6 27 30 3 1
And I want to extract a value from Row 2, based on the column of the max value of Row 1. Which in this case would give me 3.
0 Comments
Accepted Answer
Dyuman Joshi
on 25 Nov 2023
If you want the value corresponding to all occurences of the maximum value in row 1 -
%Input data
arr = [1 2 4 10 3 10
6 27 30 3 1 5];
idx1 = arr(1,:) == max(arr(1,:));
val1 = arr(2, idx1)
If you want the value corresponding to the first occurence of the maximum value in row 1 -
[~, idx2] = max(arr(1, :));
val2 = arr(2, idx2)
2 Comments
Dyuman Joshi
on 25 Nov 2023
"So i can use ~ to ignore creating a variable for the values, and just create the value for the index."
Correct, it ignores the first output argument from the function.
"Then max(arr(1, :)) will just search the first row."
Correct.
"or would idx2 just equal the value of the column?"
Yes, idx2 is equal to the value of column, where the maximum of row 1 occurs for the 1st time.
%Input data
arr = [1 2 4 10 3 10
6 27 30 3 1 5];
[~, idx2] = max(arr(1, :))
10 appears first in the 4th column of row 1.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!