Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 查找数组中最小元素的索引,而不是另一个数组(Matlab)_Arrays_Matlab_Sorting_Find - Fatal编程技术网

Arrays 查找数组中最小元素的索引,而不是另一个数组(Matlab)

Arrays 查找数组中最小元素的索引,而不是另一个数组(Matlab),arrays,matlab,sorting,find,Arrays,Matlab,Sorting,Find,我有一个数组a=[6 8 2 1 9]和b=[1 2 6]。我想要一个返回2的函数,因为a(2)=8是a的最小元素,不在b中 到目前为止,我所尝试的: [A,I] = sort(a); index = I(find(~ismember(A,b),1)) 我想要更快的东西,因为这段代码必须运行很多次,而且涉及的数组非常大 提前谢谢 这能满足您的需要吗 >> a = [6 8 2 1 9]; >> b = [1 2 6]; >> min(a(~ismember

我有一个数组a=[6 8 2 1 9]和b=[1 2 6]。我想要一个返回2的函数,因为a(2)=8是a的最小元素,不在b中

到目前为止,我所尝试的:

[A,I] = sort(a); 
index = I(find(~ismember(A,b),1))
我想要更快的东西,因为这段代码必须运行很多次,而且涉及的数组非常大


提前谢谢

这能满足您的需要吗

>> a = [6 8 2 1 9];
>> b = [1 2 6];
>> min(a(~ismember(a,b)))
ans =
     8
编辑:

哎呀,我是说

>> find(a==min(a(~ismember(a,b))),1)
ans =
     2
这会根据您的要求查找索引,而不是第一个答案给出的值。

另一个(我相信更快的)解决方案是:

a = [6 8 2 1 9];
b = [1 2 6];
[d,I] = setdiff(a,b); % d is the set difference, I is the indices of d elements in a
[m,J] = min(d);       % m is the minimum in d, J is it's index 
I(J)                  % This is the index of m in d (the value that you want)
ans = 
     2

它总是2个数组吗?只需使用
setdiff