在Matlab中选取x个最小的元素 [英] Pick x smallest elements in Matlab

查看:87
本文介绍了在Matlab中选取x个最小的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数值的矩阵,x轴表示不同的日期,y轴表示一天中的小时.在每个单元格中都有一个数字,该数字表示一天中有多少小时与一天中的某些标准相对应.这就是为什么我需要每小时进行计算,而不仅仅是在时间结束时进行计算.

I have a matrix of integer values, the x axis represent different days and y axis represents hour of the day. And in each cell is a number that indicates how many hours of the day of the day correspond some criteria of the day which is just going on. That's why I need to calculate it for every hour and not only at the end of the time.

整个问题是我必须选择5个最好的日子,这些日子的人数最少(最少).因此,基本上在矩阵中,这意味着选择该行中的5个最低数字记住列的索引,其中最小的数字是. (我需要知道它发生在哪一天).因为随着时间的流逝,时间可能会是5天,所以对整个表格进行排序会造成混乱.

The whole issue is I have then to pick 5 best days which have the lowest number (least corresponding). So basically in the matrix it means select 5 lowest numbers in the row and remember the indexes of the columns where the minimum is. (I need to know in which day it occured). Because at every time as the time goes on it can be 5 different days so sorting the whole table would do mess.

我可以通过取第一个5来使它工作起来非常丑陋,然后当我在途中发现较小的一个时,我会忘记5中的最大数字,并记住新数字的列索引.然而,这种解决方案似乎很草率. Matlab中必须有一个更好的方法来解决这个问题.

I can make it work really ugly by taking first 5 number and then when if I find smaller one on the way I will forget biggest one from the 5 and remember the index of the column for the new one. Yet this solution seems to be pretty sloppy. There has to be a better way in Matlab how to solve this.

任何能让我的生活更轻松的想法,功能吗?

Any ideas, functions that can make my life easier?

1 1 0 1 1 1 0 0 1 1

1 1 0 1 1 1 0 0 1 1

1 2 1 2 2 1 0 1 2 2

1 2 1 2 2 1 0 1 2 2

例如,在这两行中以1-10索引,在第一行中它应该返回列 3,7,8和另外两个不是很在意哪一个. 在第二行中,应返回第7、8、6、1、3列.

For example in these two rows indexed from 1-10, in the first row it should return columns 3,7,8 and two others not really caring which one. In the second row it should return columns 7,8,6,1,3.

推荐答案

A = randi(60,100,2);
[min_val,index] = sort(A(:,2),'ascend');    
output = [A(index(1:5),1) A(index(1:5),2)];

这应该对您有帮助(我想);

this should help you (I guess);

这篇关于在Matlab中选取x个最小的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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