# Find the nearest two values in all grid cells to a specified value across a 3D matrix

8 views (last 30 days)
Charlotte Findlay on 29 Sep 2022
Commented: Charlotte Findlay on 29 Sep 2022
I have a 3D matrix (A)
A = rand(7,100,100);
I want to find the two nearest values in each matrix cell to a specified value (e.g., nearestTo) based on the data from across all 7 layers of the 3D matrix without using a loop.
nearestTo = 0.1; % e.g., this is just an example could be amended if doesn't suit A above
I have managed to get a 2D matrix (100x100) with the indicies of the 1st dimension (7) which are closest to nearestTo:
[~,mindx] = min(abs(A-nearestTo), [], 1);
mindx = squeeze(mindx(1,:,:));
However, I am not sure how to:
1. Extract the row and column information and then the value of A based on the 1st dimension index information.
2. Repeat the above but extract the second closest value to nearestTo.
The output should be two matrices of 100 x 100 which have the grid cell values of the closest value to nearestTo and the second closest value to nearestTo based on the data from across all 7 layers.

Rik on 29 Sep 2022
Why squeeze the indices? Just use them to index (for which you need sub2ind).
As for the second closest: the easiest way is to mark the minimum values with inf and then search again.
rng(1) % Fix random seed to get repeatable results
A = rand(7,10,10); % reduce to 10x10 to better visualize results
nearestTo = 0.1; % e.g., this is just an example could be amended if doesn't suit A above
[~,mindx] = min(abs(A-nearestTo), [], 1);
% Determine row-column-page-pairs.
R = mindx; [~,C,P] = ndgrid(1,1:size(A,2),1:size(A,3));
mindx = sub2ind(size(A),R,C,P);
FirstResult = permute(A(mindx),[2:ndims(A) 1]); % remove the first dimension
B = A;
B(mindx) = inf; % Make sure they don't show up as minimum values
[~,mindx] = min(abs(B-nearestTo), [], 1);
% Determine row-column-page-pairs.
R = mindx; [~,C,P] = ndgrid(1,1:size(A,2),1:size(A,3));
mindx = sub2ind(size(A),R,C,P);
SecondResult = permute(B(mindx),[2:ndims(B) 1]); % remove the first dimension
FirstResult,SecondResult
FirstResult = 10×10
0.0923 0.1375 0.1075 0.1263 0.2524 0.2571 0.1133 0.2474 0.0229 0.0561 0.2045 0.3478 0.0720 0.1310 0.4657 0.1430 0.3949 0.0875 0.0052 0.0801 0.1404 0.1147 0.1954 0.3447 0.0796 0.1354 0.1256 0.1851 0.2741 0.0762 0.0850 0.2370 0.0700 0.0155 0.1610 0.1604 0.0104 0.0570 0.1950 0.1066 0.0983 0.0029 0.3174 0.1840 0.1109 0.1886 0.1456 0.5707 0.2137 0.1499 0.0183 0.1371 0.0663 0.0539 0.2082 0.0569 0.1195 0.0936 0.1109 0.1765 0.1032 0.0660 0.0665 0.2856 0.1020 0.0276 0.1468 0.0131 0.0805 0.0038 0.1300 0.1243 0.0660 0.3227 0.1445 0.0323 0.0309 0.1377 0.1262 0.3749 0.1023 0.1242 0.0140 0.3174 0.3244 0.1090 0.0273 0.2202 0.1468 0.0031 0.0500 0.0186 0.3900 0.1986 0.0987 0.3425 0.0093 0.1248 0.2107 0.1208
SecondResult = 10×10
0.1468 0.1393 0.1213 0.1351 0.2702 0.2597 0.0784 0.3713 0.0126 0.2955 0.3456 0.3489 0.0126 0.1762 0.5190 0.3002 0.4501 0.1748 0.4844 0.1923 0.0274 0.2699 0.2398 0.4599 0.0682 0.3557 0.0371 0.2404 0.3524 0.1322 0.0391 0.4081 0.1568 0.2857 0.2250 0.0304 0.0030 0.1747 0.2896 0.1081 0.1698 0.3266 0.3801 0.3152 0.1930 0.2039 0.1577 0.5944 0.2141 0.1896 0.3155 0.1723 0.2102 0.0004 0.2480 0.2120 0.1734 0.0654 0.1425 0.0198 0.2804 0.6968 0.1934 0.3766 0.1527 0.2973 0.2643 0.4769 0.2076 0.2781 0.0534 0.0283 0.2633 0.5868 0.0223 0.4650 0.3145 0.0109 0.4353 0.5167 0.1467 0.2792 0.2344 0.3821 0.5384 0.0815 0.3260 0.3200 0.1598 0.4019 0.4142 0.2330 0.4860 0.3433 0.0346 0.4288 0.2901 0.1303 0.5971 0.2203
Charlotte Findlay on 29 Sep 2022
This is great - thank you!

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by