# Finding the closest value in an array of 7 dimension vector

1 visualizzazione (ultimi 30 giorni)
Ganesh Kini il 16 Apr 2020
Modificato: Ameer Hamza il 17 Apr 2020
Hi,
I have an array period_temp (2,1,1,10,10,10,8) which gives a lot of values ​​in 2 * 1 * 1 * 10 * 10 * 8 matrix
now suppose i have one value say 3.5 which is not present in the matrix.
How do I find the closest value to 3.5?
Please guide me, I am a beginner
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Ameer Hamza il 17 Apr 2020
Modificato: Ameer Hamza il 17 Apr 2020
This code shows an example
x = rand(2,2,2);
val = 0.5;
[~,idx] = min(abs(x-val), [], 'all', 'linear');
[i1,i2,i3] = ind2sub(size(x), idx); % return index in each dimension
closest_value = x(i1,i2,i3);
The following is equivalent to the above code but convenient to use if the number of dimensions is large.
x = rand(2,2,2);
val = 0.5;
[~,idx] = min(abs(x-val), [], 'all', 'linear');
[i{1:3}] = ind2sub(size(x), idx); % return index in each dimension
closest_value = x(i{:});
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by