Find an array element's index

1 回表示 (過去 30 日間)
A VP
A VP 2017 年 10 月 2 日
コメント済み: Image Analyst 2017 年 10 月 5 日
I have a first array that has about 5000 values. I have a second array that is (first array - (0.3)*(first array(1))); For every element in the first array, I must find the value and index of the closest element in the second array. Any help is highly appreciated.
  1 件のコメント
Jan
Jan 2017 年 10 月 2 日
What have you tried so far? Is this a homework? Did you search in the forum already?

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2017 年 10 月 2 日
Did you try the obvious min() function????
[closestValue, indexOfClosestValue] = min(abs(array1-array2));
Make sure array1 and array2 are not uint8 or uint16 though. Cast to double first, if you're using one of them.

A VP
A VP 2017 年 10 月 5 日
What I have currently is this :
temp = zeros(1,length(x));
min_val = zeros(1,length(x));
index = zeros(1,length(x));
y = x- (0.3)*(x(1));
for i=1:length(x)
for j = 1 : length(x)
temp(j) = abs(y(i)-x(j));
if j == length(x)
[min_val(i), index(i)] = min(temp);
end
end
end
x is the initial array, y is the final array. y is an array formed by subtracting 30% of the first value of x from each element of x. I should be able to find the closest match of y in x and find its index in x. This logic works well for a smaller array, but the nested for loop takes forever for an array with large number of values. I have arrays with 6-digit numbers. Is there a quicker way of doing this?
  1 件のコメント
Image Analyst
Image Analyst 2017 年 10 月 5 日
But your temp is always a constant. Since y = x-constant, then y-x is x-(x-constant) which equals the constant. So it's the same everywhere. All elements have the same value, which is 0.3*x(1).

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by