Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The values you obtain are the exact centroids for those objects. So you might want to define what you'd expect to get as results.</p> <p>To make this more clear, I've colored the objects in your matrix. By the symmetry on your cartesian grid, there should be an equal number of points to the left and right of your centroid and the same for above/below the centroid. I've drawn a figure with your objects colored in, together with lines to represent the horizontal and vertical center lines. Those are lines for which, we have an equal number of points to the left/right (or above/below) of them that are part of a certain object.</p> <p>Their intersection is the centroid, so you can see for object 5 (blue one) that the centroid is at <code>(8, 14)</code>. For the other two objects, these center lines do not lie on the integer grid you have: the red object (1) has its centroid at <code>(12.5, 7)</code> which is also the outcome of your code and the green object (2) is centered around <code>(2.5, 10.5)</code>.</p> <p>You will either have to live with inaccuracy introduced by rounding your centroids (e.g. <code>round(cx)</code>) or you will have to live with the non-integer coordinates of the centroids.</p> <p><img src="https://i.stack.imgur.com/9OoQ5.png" alt="Graphical representation"></p> <p>Next to that, I also recommend you vectorize your code as oli showed: this allows you to run your code faster and it is easier to understand when you are somewhat familiar with MATLAB than <code>for</code> loops.</p> <p>Perhaps a little note with regard to your string representation: don't use <code>%d</code> for non-integers, as you see that will cause real numbers to be displayed in scientific notation. I think it's clearer if you use something like <code>%0.2f</code> as your format string.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload