在数组中找到n个最小值 [英] Find n minimum values in an array
问题描述
我正在使用Matlab 2012a.
我有一个由k个细胞组成的阵列(例如1000个).我需要找到该数组的5个最低值,并需要对X和Y中的这些值进行平均.每个人都有一个想法怎么做?
假定您有X和Y数组,并且要查找五个最低的Y值:
[m mi] = sort(Y);
lowest5index = mi(1:5);
lowest5Y = Y(lowest5index);
lowest5X = X(lowest5index);
meanYlowest5 = mean(lowest5Y);
meanXlowest5 = mean(lowest5X);
说明:
具有两个输出参数的sort
命令返回排序后的数组(在m
中)和原始数组中的索引(在mi
中).前五个索引mi(1:5)
对应于五个最低值.对X和Y取这些值的mean
将完成我们想要的.如果我不明白您的问题陈述,请澄清您的问题,我将再进行处理.
I am using Matlab 2012a.
I have an array of k cells (say 1000). I need to find the 5 lowest values of this array and need to do an average of those values in X and Y.
Anyone has an idea how to do that?
Assuming you have arrays X and Y, and you want to find the five lowest Y values:
[m mi] = sort(Y);
lowest5index = mi(1:5);
lowest5Y = Y(lowest5index);
lowest5X = X(lowest5index);
meanYlowest5 = mean(lowest5Y);
meanXlowest5 = mean(lowest5X);
Explanation:
The sort
command with two output parameters returns both the sorted array (in m
) and the indices in the original array (mi
). The first five indices mi(1:5)
correspond to the five lowest values. Taking the mean
of these values for both X and Y will do what we want. If I didn't understand your problem statement, please clarify your question and I will take another shot at it.
这篇关于在数组中找到n个最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!