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

  • SEARCH
  • Home
  • 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 8584661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:49:29+00:00 2026-06-11T21:49:29+00:00

I am trying to calculate distance between two matrices.For eg. it goes like this.

  • 0

I am trying to calculate distance between two matrices.For eg. it goes like this.

Matrix-M1

1 2
3 4 
5 6 
7 8
9 0

Matrix-M2

1 1
2 2

Distance matrix ( d1 is distance between M1 1st row and M2 1st row,d2 is distance between M1 1st row and M2 2nd row.)

  d1 d2
  d3 d4 
  d5 d6 
  d7 d8 
  d9 d10

I tried the below code but doesn’t work.Please help me.

    #include<stdio.h>
    #include<math.h>

    int calcdist(int ,int ,int ,int);

    int main()
    {
            int points[5][2];
            int centroid[2][2];
            int distance[5][2];
            int i,j,k;
            printf("Enter the points:\n");
            for(i=0;i<5;i++)
            for(j=0;j<2;j++)
            scanf("%d",&points[i][j]);

            printf("Displaying the points.\n");
            for(i=0;i<5;i++)
            {
                    for(j=0;j<2;j++)
                    printf("%d\t",points[i][j]);
                    printf("\n");
            }

            printf("Enter the centroids:\n");
            for(i=0;i<2;i++)
            for(j=0;j<2;j++)
            scanf("%d",&centroid[i][j]);

            printf("Displaying the centroids.\n");
            for(i=0;i<2;i++)
            {
                    for(j=0;j<2;j++)
                    printf("%d\t",centroid[i][j]);
                    printf("\n");
            }

            for(i=0;i<5;i++)
            {
                    k=0;
                    for(j=0;j<2;j++)
                    {
                            distance[i][j]=calcdist(points[i][j],points[i][j+1],centroid[k][j],centroid[k][j+1]);
 }
        }

        printf("The distance between the points and centroids are as below.\n");
        for(i=0;i<5;i++)
        {
                for(j=0;j<2;j++)
                {
                        printf("%d\t",distance[i][j]);
                }
                printf("\n");
        }

        return 0;
}

int calcdist(int a,int b,int c,int d)
{
        int dist;
        dist=sqrt(pow((c-a),2)+pow((d-b),2));
        return dist;
}
  • 1 1 Answer
  • 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. Editorial Team
    Editorial Team
    2026-06-11T21:49:31+00:00Added an answer on June 11, 2026 at 9:49 pm

    Your iteration when calculating is out of bounds (etc.):

    I.e.:

            fprintf(stderr,
                "dist[%d][%d] :: "
                "pt[%d][%d](%d), "
                "pt[%d][%d](%d), "
                "cn[%d][%d](%d), "
                "cn[%d][%d](%d) => "
                "%d\n"
                ,
                i, j,
                i, j, points[i][j],
                i, j + 1, points[i][j + 1],
                k, j, centroid[k][j],
                k, j + 1, centroid[k][j + 1],
                distance[i][j]
            );
    

    You get:

                          a            b            c            d    
    dist[0][0] ::pt[0][0](1), pt[0][1](2), cn[0][0](1), cn[0][1](1) => 1
    dist[0][1] ::pt[0][1](2), pt[0][2](3), cn[0][1](1), cn[0][2](2) => 1
    dist[1][0] ::pt[1][0](3), pt[1][1](4), cn[0][0](1), cn[0][1](1) => 3
    dist[1][1] ::pt[1][1](4), pt[1][2](5), cn[0][1](1), cn[0][2](2) => 4
    dist[2][0] ::pt[2][0](5), pt[2][1](6), cn[0][0](1), cn[0][1](1) => 6
    dist[2][1] ::pt[2][1](6), pt[2][2](7), cn[0][1](1), cn[0][2](2) => 7
    dist[3][0] ::pt[3][0](7), pt[3][1](8), cn[0][0](1), cn[0][1](1) => 9
    dist[3][1] ::pt[3][1](8), pt[3][2](9), cn[0][1](1), cn[0][2](2) => 9
    dist[4][0] ::pt[4][0](9), pt[4][1](0), cn[0][0](1), cn[0][1](1) => 8
    dist[4][1] ::pt[4][1](0), pt[4][2](1), cn[0][1](1), cn[0][2](2) => 1
    

    centroid[0][2] is out of bounds for centroid as well as i.e. point[1][2].

    Also; the calcdist function return an int which means all distances are rounded towards zero. sqrt(61) = 7.810 => 7

    To make a long story short; try something like:

            distance[i][j] = calcdist(
                        points[i][0],
                        points[i][1],
                        centroid[j][0],
                        centroid[j][1]
                    );
    

    I.e.:

    $ gcc -Wall -Wextra -pedantic -std=c89 -ggdb -DDEBUG -o dist dist.c -lm

    Code:

    #include <stdio.h>
    #include <math.h>
    
    int calcdist(int, int, int, int);
    
    void prnt_2dmatrix(const char *title, int m[][2], int len)
    {
        int i;
    
        fprintf(stdout, "%s {\n", title);
        for (i = 0; i < len; i++) {
            fprintf(stdout,
                "  %d    %d\n",
                m[i][0],
                m[i][1]
            );
        }
        fprintf(stdout, "}\n");
    }
    
    int main(int argc, char *argv[])
    {
        int points[5][2] = {{1,2},{3,4},{5,6},{7,8},{9,0}};
        int centroid[2][2] = {{1,1},{2,2}};
        int distance[5][2] = {{0},{0}};
        int i, j;
    
        if (argc > 1 && argv[1][0] == 't') {
            printf("Using internal test vectors.\n");
        } else {
            printf("Enter the points:\n");
            for (i = 0; i < 5; i++)
                for (j = 0; j < 2; j++)
                    scanf("%d", &points[i][j]);
    
            printf("Enter the centroids:\n");
            for (i = 0; i < 2; i++)
                for (j = 0; j < 2; j++)
                    scanf("%d", &centroid[i][j]);
        }
    
        prnt_2dmatrix("Points", points, 5);
        prnt_2dmatrix("Centroids", centroid, 2);
    
        for (i = 0; i < 5; i++) {
            for (j = 0; j < 2; j++) {
                distance[i][j] = calcdist(
                            points[i][0],
                            points[i][1],
                            centroid[j][0],
                            centroid[j][1]
                        );
    #ifdef DEBUG
                fprintf(stderr,
                    "dist[%d][%d] = "
                    "pt[%d][%d](%d), "
                    "pt[%d][%d](%d), "
                    "cn[%d][%d](%d), "
                    "cn[%d][%d](%d) => "
                    "%d\n"
                    ,
                    i, j,
                    i, 0, points[i][0],
                    i, 1, points[i][1],
                    j, 0, centroid[j][0],
                    j, 1, centroid[j][1],
                    distance[i][j]
                );
            }
            printf("\n");
    #else
            }
    #endif
        }
    
        prnt_2dmatrix("Distance points <-> centroids", distance, 5);
    
        return 0;
    }
    
    int calcdist(int a, int b, int c, int d)
    {
    #ifdef DEBUG
        fprintf(stderr,
            " sqrt((%2d - %2d)^2 + (%2d - %2d)^2) =\n"
            " sqrt(       %2d^2 +        %2d^2) =\n"
            " sqrt(         %2.f +          %2.f) =\n"
            " sqrt(                       %2.f) = "
            " %.3f\n"
            ,
            c, a, d, b,
            c - a, d - b,
            pow(c - a, 2), pow(d - b, 2),
            pow(c - a, 2) + pow(d - b, 2),
            sqrt(pow((c - a), 2) + pow((d - b), 2))
        );
    #endif
        return sqrt(pow((c - a), 2) + pow((d - b), 2));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to calculate distance between two corelocation objects like this: float meters
In the program below, I am trying to calculate the distance between two points.
I'm trying to calculate the distance between two points. The two points I stored
In the code below I am trying to calculate the distance between two cities.
I am trying to calculate the distance between two map point using Google Map
I'm trying to calculate a distance between two sets of coordinate points in an
I am trying to calculate the distance between two points (Pins) on the iPhone
I'm trying to calculate the distance between two places user Core Location. I've found
I am trying to calculate the shortest distance between any two numbers in an
I am trying to see if the calculated distance between two points is smaller

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.