2

I' have 2 matrices: a binary one of size 286 x 720 where 0 = no airport and 1 = a pixel where an airport is located. Secondly, I have a population matrix with equal size but with the total amount of people that live in each cell. Now I want to find the pixel where there is an airport and where the most people are living. So how can I find which airport pixel has the highest amount of people living in it (the maximum value of population when only airport pixels are considered)?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
user5618251
  • 317
  • 1
  • 9

1 Answers1

3
Airport = randi(2,286,720)==1; %// airport grid
People = randi(1e3,286,720); %// people grid

PeopleOnAirport = People(Airport); %// logical mask
MaxPeople = max(PeopleOnAirport(:)); %// find maximum
[Pixel(:,1),Pixel(:,2),~] = find(People==MaxPeople); %// finds the location of the maximum.

You Airport matrix is a binary one, thus you can use it as a logical index to the People matrix. Then a simple call to max will suffice to find the maximum number of people, which can be traced to a matrix element using find. The location is stored in Pixel, with the first column being the row number and the second the column number of the location. If there's more than one location which contains that maximum number of people, each row of Pixel contains a location.

Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • To use logical indexing, you have to create a matrix of logicals: `Airport = randi(2,286,720)==1;` – Daniel Jan 18 '16 at 14:17
  • Better use `[Pixel(:,1),Pixel(:,2),~] = find(People==MaxPeople);` just in case the maximum is hit multiple times – Daniel Jan 18 '16 at 14:20
  • @Daniel thanks, I updated it. I should've noticed your first comment's content. I updated and added in your suggestion, thanks – Adriaan Jan 18 '16 at 14:23