Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 99007

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:18:18+00:00 2026-05-11T00:18:18+00:00

This is in continuation with the question posted here: Finding the center of mass

  • 0

This is in continuation with the question posted here: Finding the center of mass on a 2D bitmap which talked about finding the center of mass in a boolean matrix, as the example was given.

Suppose now we expand the matrix to this form:

0 1 2 3 4 5 6 7 8 9 1 . X X . . . . . . 2 . X X X . . X . . 3 . . . . . X X X . 4 . . . . . . X . . 5 . X X . . . . . . 6 . X . . . . . . . 7 . X . . . . . . . 8 . . . . X X . . . 9 . . . . X X . . . 

As you can see we now have 4 centers of mass, for 4 different clusters.

We already know how to find a center of mass given that only one exists, if we run that algorithm on this matrix we’ll get some point in the middle of the matrix which does not help us.

What can be a good, correct and fast algorithm to find these clusters of mass?

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:18:19+00:00Added an answer on May 11, 2026 at 12:18 am

    I think I would check each point in the matrix and figure out it’s mass based on it’s neighbours. The mass for points would fall with say the square of the distance. You could then pick the top four points with a minimum distance from each other.

    Here’s some Python code I whipped together to try to illustrate the approach for finding out the mass for each point. Some setup using your example matrix:

    matrix = [[1.0 if x == 'X' else 0.0 for x in y] for y in '''.XX...... .XXX..X.. .....XXX. ......X.. .XX...... .X....... .X....... ....XX... ....XX...'''.split('\n')]  HEIGHT = len(matrix) WIDTH = len(matrix[0]) Y_RADIUS = HEIGHT / 2 X_RADIUS = WIDTH / 2 

    To calculate the mass for a given point:

    def distance(x1, y1, x2, y2):   'Manhattan distance http://en.wikipedia.org/wiki/Manhattan_distance'   return abs(y1 - y2) + abs(x1 - x2)  def mass(m, x, y):   _mass = m[y][x]   for _y in range(max(0, y - Y_RADIUS), min(HEIGHT, y + Y_RADIUS)):     for _x in range(max(0, x - X_RADIUS), min(WIDTH, x + X_RADIUS)):       d = max(1, distance(x, y, _x, _y))       _mass += m[_y][_x] / (d * d)   return _mass 

    Note: I’m using Manhattan distances (a k a Cityblock, a k a Taxicab Geometry) here because I don’t think the added accuracy using Euclidian distances is worth the cost of calling sqrt().

    Iterating through our matrix and building up a list of tuples like (x, y, mass(x,y)):

    point_mass = [] for y in range(0, HEIGHT):   for x in range(0, WIDTH):     point_mass.append((x, y, mass(matrix, x, y))) 

    Sorting the list on the mass for each point:

    from operator import itemgetter point_mass.sort(key=itemgetter(2), reverse=True) 

    Looking at the top 9 points in that sorted list:

    (6, 2, 6.1580555555555554) (2, 1, 5.4861111111111107) (1, 1, 4.6736111111111107) (1, 4, 4.5938888888888885) (2, 0, 4.54) (4, 7, 4.4480555555555554) (1, 5, 4.4480555555555554) (5, 7, 4.4059637188208614) (4, 8, 4.3659637188208613) 

    If we would work from highest to lowest and filter away points that are too close to already seen points we’ll get (I’m doing it manually since I’ve run out of time now to do it in code…):

    (6, 2, 6.1580555555555554) (2, 1, 5.4861111111111107) (1, 4, 4.5938888888888885) (4, 7, 4.4480555555555554) 

    Which is a pretty intuitive result from just looking at your matrix (note that the coordinates are zero based when comparing with your example).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Seems like the issue with the missing namespace prefix for… May 11, 2026 at 1:57 pm
  • added an answer You might want to take a look here and here:… May 11, 2026 at 1:57 pm
  • added an answer Levenshtein Python extension and C library. https://github.com/ztane/python-Levenshtein/ The Levenshtein Python… May 11, 2026 at 1:57 pm

Related Questions

This is in reference to my other question Auto Clearing Textbox . If I
This is in C#, I have a class that I am using from some
This is in regards to a situation where Session is used to store some
This is in relation to this question I am hosting this WCF service in
When presenting preformatted text on the web (e.g. code samples), line wrapping can be
Will the CAN interface card likely be installed as a COM port? How do
This is a continuation question from a previous question I have asked I now
Ok, I'm having issues with the linker on my current project (This is a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.