Calculate average between min and max numbers in column 3 depending on values in column 2, for each value in column 1
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Voss
2022-1-19
"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
2022-1-20
编辑:Voss
2022-1-20
@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
采纳的回答
更多回答(2 个)
Image Analyst
2022-1-19
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 个评论
Voss
2022-1-19
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!