Matlab 在两个数组比较期间,第一个数组的索引大于
我有两个数组和阈值Matlab 在两个数组比较期间,第一个数组的索引大于,matlab,Matlab,我有两个数组和阈值 threshold=[10 22 97] values=[99 23 77 11 8 10] 我想输出idx,这样阈值(idx-1)无循环通常意味着您将通过增加峰值内存来提高速度。试试这个: threshold = [10 22 97]; values = [99 23 77 11 8 10]; %// Do ALL comparisons A = sum(bsxfun(@gt, values.', threshold)); %// Find the in
threshold=[10 22 97]
values=[99 23 77 11 8 10]
我想输出idx,这样阈值(idx-1)
无循环通常意味着您将通过增加峰值内存来提高速度。试试这个:
threshold = [10 22 97];
values = [99 23 77 11 8 10];
%// Do ALL comparisons
A = sum(bsxfun(@gt, values.', threshold));
%// Find the indices and the ones before
R = max(1, [A; A-1]);
%// The array you want
R(:).'
如果内存不足,只需使用循环,然后用find
替换内部循环
循环并不都那么糟糕,你知道(如果你有MATLAB>R2008)。理论上,上面的解决方案甚至不应该比使用find
的循环更快,但哦,好吧……评测是关键:)您可以使用命令,只需稍微调整阈值
数组
>> threshold=[-inf 10 22 97 inf];
>> values=[99 23 77 11 8 10];
>> [~, output] = histc( values, threshold+.1 )
output =
4 3 3 2 1 1
阈值的修改
是由于bin边界决策的“小于”/“小于等于”类型的比较。带j的内循环也可以用find函数代替。即,查找(阈值>值(i),1,'first');为什么要避免循环?你想要一个简单的方法,但你已经有了一个天真的方法。天真的方法和简单的方法有什么不同?矩阵真的很大。for循环非常慢。您的值没有排序…我想这是一个输入错误?我想要一个更快的matlab友好代码。@RodyOldenhuis是的。。。看到你的答案我很惊讶-我真的以为你领先了我,我正要删除我的答案…似乎我的答案在未排序的值的上下文中是错误的
…我将删除我的并喝一些咖啡:)@RodyOldenhuis请不要删除你的答案。这是一个使用bsxfun
进行比较的好例子@Shai:如果你坚持的话。
threshold = [10 22 97];
values = [99 23 77 11 8 10];
%// Do ALL comparisons
A = sum(bsxfun(@gt, values.', threshold));
%// Find the indices and the ones before
R = max(1, [A; A-1]);
%// The array you want
R(:).'
>> threshold=[-inf 10 22 97 inf];
>> values=[99 23 77 11 8 10];
>> [~, output] = histc( values, threshold+.1 )
output =
4 3 3 2 1 1