在数组中找到n个最小值 [英] Find n minimum values in an array

查看:161
本文介绍了在数组中找到n个最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆