Info

This question is closed. Reopen it to edit or answer.

Non-loop comparison of 3 - D matrices using "find"?

1 view (last 30 days)
Joseph Koval
Joseph Koval on 12 Mar 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
I have an 3-D matrix that is 21 X 751 x 1500. The dimensions are height above sea level (21) X grid row (751) X grid column (1500). I have another matrix which is 1 X 751 X 1500 which, for each grid point, contains a "flag" value that falls somewhere in the range of the 21 heights at each point in the 21 X 751 x 1500 grid. For each point in the 751 X 1500 grid, I want to extract the index in the height dimension of the height value that is just below that of the "flag" value from the 1 X 751 X 1500 grid. For example, at grid location 200 X 200, I have an array of 21 height above ground values that look like this:
17.33755
141.1017
265.9034
271.7307
392.0424
562.1868
778.3396
998.8344
1223.753
1453.018
1927.371
2428.01
2963.5
3535.615
4145.533
4798.753
5501.632
6261.408
7090.483
8004.781
9030.345
In my 1 X 751 X 1500 grid at 200 X 200, my "flag" value is 204. I am trying to figure out how to extract the index of the values in the 21 height above ground dimension at each of the 751 X 1500 points that is closest to the "flag" value without exceeding it. In this example, the code should return a value of "2", since 141.1017 is closest to 204 without exceeding it.
I know I can do this by looping over every grid point, but I'm wondering if there's a quicker way to do it using "find", etc. Ideally, the code would return a 1 X 751 X 1500 matrix containing indicies of the value closest to the "flag" value without exceeding it.

Answers (2)

Andrei Bobrov
Andrei Bobrov on 12 Mar 2018
Edited: Andrei Bobrov on 12 Mar 2018
Let A - your 3-D matrix with size that is 21 x 751 x 1500
B - your "flag" array with size that is 1 x 751 x 1500
for MATLAB >= R2016b
C = A;
C(A - B >= 0) = inf;
[~,out] = min(C);

Joseph Koval
Joseph Koval on 12 Mar 2018
Thanks Andrei...your suggestion pushed me in the right direction...I had to modify a bit to get it to work. Here's my final version:
C = sortedheights;
C(sortedheights - hgtsfcplus150 >= 0) = NaN ;
[~,indexbelow] = max(C);

Tags

Community Treasure Hunt

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

Start Hunting!