Group Abstract Group Abstract

Message Boards Message Boards

1
|
8.1K Views
|
5 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Check cities contained within a circle

5 Replies

It's not clear to me what you mean by "total population inside the previous circle".

Given a list of cities (those in the circle or any other list), you can eliminate elements of the list using Delete.

For example take the list of populations of a given list of cities:

populations = EntityValue[cities, "Population"]

Then something like this will add all populations except those at positions i and j:

total[i_, j_] := Total[Delete[populations, {{i}, {j}}]]

(The need of using double lists is a detail of the syntax of Total, not relevant here.) If the list of cities has length n, then the population matrix you want is something like

Table[total[i, j], {i, n}, {j, n}]

Note that on the diagonal we are removing only the population of city i, and we are removing it once, not twice.

Thank you both @Jose M. Martin-Garcia and @Henrik Schachner, your replies have been very helpful!

I am trying now to construct a matrix S, where the element ij contains the total population inside the previous circle, excluding the ones of cities i and j.

I have found that the populations can be obtained by using:

population = CityData[#, "Population"] & /@ cities

But I am not sure how to construct that matrix. Any idea would be much appreciated.

POSTED BY: Henrik Schachner

Hello David,

I'd suggest to use entities for the cities:

cities = {
   Entity["City", {"Istanbul", "Istanbul", "Turkey"}], 
   Entity["City", {"Moscow", "Moscow", "Russia"}], 
   Entity["City", {"London", "GreaterLondon", "UnitedKingdom"}], 
   Entity["City", {"Berlin", "Berlin", "Germany"}], 
   Entity["City", {"Madrid", "Madrid", "Spain"}]
}

Define also the two cities of reference:

madrid = Entity["City", {"Madrid", "Madrid", "Spain"}]
berlin = Entity["City", {"Berlin", "Berlin", "Germany"}]

Compute the distance between them (units depend on whether you are, so in the US we get miles):

In[]:= dist = GeoDistance[madrid, berlin]
Out[]= Quantity[1162.07, "Miles"]

Now you can query:

In[]:= Select[cities, GeoDistance[madrid, #] < dist &]
Out[]= {
   Entity["City", {"London", "GreaterLondon", "UnitedKingdom"}], 
   Entity["City", {"Madrid", "Madrid", "Spain"}]
}

This performs an independent call to WA per city, which will be slow for long lists. We can do better in those cases by fetching all positions at once in advance.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard