在Matlab中找到最接近的较小值

时间:2019-10-10 13:45:49

标签: arrays matlab

我有两个大小不同的向量。举个例子:

... com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.system): Session adoption is only allowed in user domains.

对于Triggs = [38.1680, 38.1720, 38.1760, 38.1800, 38.1840, 38.1880, 38.1920, 38.1960, 38.2000, 38.2040, 38.2080, 38.2120, 38.2160, 38.2200, 38.2240, 38.2280, 38.2320, 38.2360, 38.2400, 38.2440, 38.2480, 38.2520, 38.2560, 38.2600, 38.2640, 38.2680] Peaks = [27.7920, 28.4600, 29.1360, 29.8280, 30.5200, 31.2000, 31.8920, 32.5640, 33.2600, 33.9480, 34.6520, 35.3680, 36.0840, 36.7680, 37.5000, 38.2440, 38.9920, 39.7120, 40.4160, 41.1480, 41.8840, 42.5960, 43.3040, 44.0240, 44.7160, 45.3840, 46.1240, 46.8720, 47.6240, 48.3720, 49.1040, 49.8080, 50.5200, 51.2600] 中的每个元素,我需要在Triggs中找到最接近的较小元素。

也就是说,如果Peaks,我需要找到等于Triggs(1) == 38.1680的列号(Peaks的第15个元素)。

仅需100%清除,最接近的元素当然就是下一个元素,即Peaks(15)。对我来说那不行。我将始终需要数组左侧的那个。

到目前为止,我有这个:

38.2440

但是,这可能会给我不正确的值,即比for i = 1:length(triggersStartTime) [~,valuePosition] = (min(abs(Peaks-Triggs(i)))) end 大一个,对吧?

作为解决方案,我认为我可以这样做:

Triggs(i)

有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

这可以如下向量化的方式完成(请注意,中间矩阵public OwnerWithholding CalculationMethod1(Reservation r, SqlConnection conn) { OwnerWithholding result = new OwnerWithholding(); // result.ManagementFeePct = some data from Fees table in DB + value //from another db - constant value.. // Also with other properties - with some operations on data //result.TotalManagementFee = .. // result.OperationalFeesPct = .. // result. TotalOperationalFees = .. return result; } 可能很大)。如果没有满足条件的数字,则将输出设置为d

NaN

如果确保始终有一个满足条件的数字,则可以删除最后一行和变量d = Triggs(:).'-Peaks(:); % matrix of pair-wise differences. Uses implicit expansion d(d<=0) = NaN; % set negative differences to NaN, so they will be disregarded [val, result] = min(d, [], 1); % for each column, get minimum value and its row index result(isnan(val)) = NaN; % if minimum was NaN the index is not valid

val

答案 1 :(得分:0)

我认为这应该对您有所帮助

 temp=sort(abs(Peaks-Triggs));
 lowest=find(abs(Peaks-Triggs)==temp(1))