If I have two 2-D datasets with identical dimension sizes, how can I filter data from one at certain gridpoints where the other dataset meets a certain condition?

1 view (last 30 days)
I'm dealing with NetCDF data which I read into a MATLAB array. They are both 141x301 double matrices. One of them has a numeric indicator for certain objects; think of it as a map that has contiguous shapes, and each shape has a unique identifier that is between 1 and 100, with all blank spaces as 0. Additionally, there can be multiple shapes on the same map. The OTHER matrix contains values for each grid point, and I want to find the max value for each distinct shape.
Is there a way to find the max for each shape without using a double for-loop? I will do that if I have to but I want to avoid that because I'm already doing this over many arrays and I don't want to end up with tons of nested for-loops.

Accepted Answer

KSSV
KSSV on 7 Aug 2019
A = randi([1 5],5,5) ; % shapes
B = rand(5) ; % data
[c,ia,ib]= unique(A) ;
N = length(c) ;
% GEt max values
iwant = zeros(N,1) ;
for i = 1:N
iwant(i) = max(B(ib==i)) ;
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!