Calculate average between min and max numbers in column 3 depending on values in column 2, for each value in column 1
2 views (last 30 days)
Show older comments
Hello. I was wondering if someone could help me with this:
Let's say you have a 2000 x 3 matrix, where all the elements are integers. In column 1, the values can be between 1 and 16. How can we select one element in column 1 (e.g. 7), then for those rows only, select the rows with the highest 5 values in column 2, and for these rows only, select their elements in column 3 and calculate the average between the min and max values (output).
Thanks!
2 Comments
Voss
on 19 Jan 2022
"select the rows with the highest 5 values in column 2"
Does this mean, "select the 5 rows with the highest value in column 2"?
Or does it mean, "select all rows whose value in column 2 is among the 5 highest unique values in column 2"?
(It's understood that we're talking about only those rows that match the selected element in column 1 already.)
Voss
on 20 Jan 2022
Edited: Voss
on 20 Jan 2022
@Neuro FYI, the accepted answer here selects the 5 rows with the highest value in column 2, whereas the accepted answer in another question you posted selects all rows whose value in column 2 is among the 5 highest unique values in column 2. You should be aware of this distinction because it seems likely that these questions are related.
To illustrate the distinction:
X = [1 2 3 4 5 5 5 6 6 6 6];
maxk(X,5) % 5 highest values
uX = unique(X);
X(X >= uX(end-4)) % among the 5 highest unique values
Accepted Answer
More Answers (2)
Image Analyst
on 19 Jan 2022
Just take it one step at a time. Try this.
% Let's say you have a 2000 x 3 matrix, where all the elements are integers.
% In column 1, the values can be between 1 and 16. How can we select one element in column 1 (e.g. 7),
% then for those rows only, select the rows with the highest 5 values in column 2,
% and for these rows only, select their elements in column 3 and
% calculate the average between the min and max values (output).
m = randi([1, 16], 2000, 3);
% Find rows with column 1 = 7
rows1Equals7 = m(:, 1) == 7;
% Get values from column 2 in those rows
col2 = m(rows1Equals7, 2)
% Select the rows with the highest 5 values
[highValues, rowsWithHighValues] = maxk(col2, 5)
% Get the values from column 3 in those 5 rows.
col3 = m(rowsWithHighValues, 3)
% Get the average "between" the max and min. Not sure what "between" means
% so I'm taking the average of the min and max.
max3a = mean([min(col3), max(col3)])
% Alternative: Get the average of ALL 5 values
max3b = mean(col3)
0 Comments
Voss
on 19 Jan 2022
matrix = randi([1 16],2000,3);
search_number = 7;
idx_1 = matrix(:,1) == search_number;
col2_vals = unique(matrix(idx_1,2));
idx_2 = idx_1 & matrix(:,2) >= col2_vals(end-4);
col3_min = min(matrix(idx_2,3));
col3_max = max(matrix(idx_2,3));
col3_avg = (col3_min+col3_max)/2;
disp(col3_min);
disp(col3_max);
disp(col3_avg);
0 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing 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!