如何在其他矩阵中捕捉到立即数的索引? [英] How to catch the index of immediate greater number in other matrix?

查看:73
本文介绍了如何在其他矩阵中捕捉到立即数的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑示例

a=rand(5,1)
b=rand(5,1);
bs=sum(b);
B=b./bs;
cB=cumsum(B)

%OUTPUT

a =

0.7803
0.3897
0.2417
0.4039
0.0965


cB =

0.0495
0.4030
0.7617
0.9776
1.0000

现在我希望数字在cB中的位置立即大于a中的数字. 也就是说我想要5个与a中的每个数字相对应的位置. 所以我的输出应该是

now i want the position of the number in cB which is immediately greater than the number in a. that is to say i want 5 positions corresponding to each number in a. So my output should be

P= [4;2;2;3;2]

请帮助.

推荐答案

其他人的建议很不错,但是两者都忽略了要点,因为它们在解决大问题时效率低下.这是histc最好完成的工作. (我承认,histc显然不是解决该问题所需的工具.我希望他们选择了一些更明显的名称,因为似乎很少有人对此有所了解.histc用于直方图,也用于样条曲线的评估. )

The suggestions by the others are decent, but both miss the point as they are inefficient for large problems. This is a job best done by histc. (I admit that histc is not obviously the tool you would lookfor to solve this problem. I wish they had chosen some more obvious name, because few people seem to know about it. histc is used for histogramming, but also for the evaluation of splines.)

对于您的测试用例...

For your test case...

a = [0.7803 0.3897 0.2417 0.4039 0.0965];
cB = [0.0495 0.4030 0.7617 0.9776 1.0000];

[~,b] = histc(a,cB);
b = b + 1
b =
     4     2     2     3     2

Histc返回目标正下方的元素的索引,因此您需要添加1.

Histc returns the index of the elements just UNDER your target, so you need to add 1.

Eitan指出,如果c b不是单调的,则存在问题.但是,在那种情况下,任何解决方案都存在问题,因为解决方案不是唯一的.如果没有提供更多信息(例如,您希望获得第一个或最后一个合格的索引),那么对于完全通用的cB,就没有有效的答案.例如,如果我们有:

Eitan points out that IF cB is not monotonic, then there are problems. However, there are problems with ANY solution in that case, since the solution will not be unique. Without more information provided, such as do you desire the first or last qualifying index, there is no valid answer to the problem for completely general cB. For example, if we had:

cB = [1 3 2 4];
a = 2.5;

有两种可能的解决方案,一个可能会得出,因此索引为2或4.请注意,在将histc作为MATLAB中的工具提供之前,我曾经必须为客户提供针对此问题的解决方案. .例如,在样条代码中,常见的问题是找到一个点落入的打结间隔.当然,这些打结必须按排序的顺序排列. (我将忽略重复中断的问题.)在样条曲线代码中也有一种情况,仓边不是按排序顺序排列的,这是为样条曲线求反值的情况,然后不必完全单调的.在这种情况下,最合适的解决方案是解决问题.只有客户会做出决定.

There are two possible solutions one might arrive at, thus an index of either 2 or 4. Note that I have had to provide solutions for exactly this problem in the past for clients, long before histc was provided as a tool in MATLAB. For example, in splines codes, the common problem is of locating the knot interval a point falls in. Of course then, the knots must lie in a sorted order. (I'll ignore the issue of replicated breaks.) There is also a case in splines codes where the bin edges are not in a sorted order, and this is the case of finding the inverse value for a spline, which then need not be monotonic at all. In that case, it may well be appropriate to solve for the rightmost solution. Only the customer would make that decision.

由于示例中生成的cB严格是单调的,所以我只能假定单调性是此问题假设的一部分.

Since cB was generated in the example to be strictly monotone, I can only presume that monotonicity is part of the assumptions for this question.

这篇关于如何在其他矩阵中捕捉到立即数的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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