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 8571943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:53:39+00:00 2026-06-11T18:53:39+00:00

I want to build a square matrix. Let’s suppose We have this matrix called

  • 0

I want to build a square matrix. Let’s suppose We have this matrix called nodes

1 4.3434  3.4565
2 6.2234  5.1234
3 10.4332 2.3243
4 7.36543 1.1434

where the column 2 and 3 represents position x and y of nodes n

and a matrix called heads where its elements are some elements of nodes matrix

2 6.2234 5.1234
3 10.4332 2.3243

I created this function to build the matrix of the distance of every nodes from the heads

function [distances] = net_dist(nodes,heads)
nnodes = length(nodes(:,1));
distances = zeros(nnodes);
for i = 1 : nnodes
    for j = 1 : nnodes
        if nodes(i,1) == nodes(j,1) && ismember(nodes(j,1),heads(:,1))
            distances(i,j) = sqrt((nodes(i,2) - nodes(j,2))^2 + (nodes(i,3) - nodes(j,3))^2);
        elseif (nodes(i,1) == nodes(j,1) || nodes(i,1) ~= nodes(j,1)) && ismember(nodes(j,1),heads(:,1))
            distances(i,j) = sqrt((nodes(i,2) - nodes(j,2))^2 + (nodes(i,3) - nodes(j,3))^2); 
        elseif (nodes(i,1) == nodes(j,1) || nodes(i,1) ~= nodes(j,1)) && ~ismember(nodes(j,1),heads(:,1))
            distances(i,j) = 1E9;
        end
    end
end
return;

This function should return the distance of every nodes from a heads. The positions between nodes that aren’t heads are filled with number 1E9. I don’t understand why when I execute this function instead to receive sqrt values I receive all 0.

Definitely I would obtain such similar thing

   1   2   3   4 
 1 1E9 d   d   1E9
 2 1E9 0   d   1E9
 3 1E9 d   0   1E9
 4 1E9 d   0   1E9
  • 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-11T18:53:40+00:00Added an answer on June 11, 2026 at 6:53 pm

    You do not get zeros, you get correct distances. You probably think you get zeros, because 1e9 is a large value and when you try to print your matrix you get

    distances =
    
    1.0e+09 *
    
    1.0000    0.0000    0.0000    1.0000
    1.0000         0    0.0000    1.0000
    1.0000    0.0000         0    1.0000
    1.0000    0.0000    0.0000    1.0000
    

    You can see that the two 0-entries are true zeros, while the others are approximately 0 down to 4 digits after the coma. Try to print one of the matrix elements and see they are not zeros

    distances(1,2)
    
    ans =
    
    2.5126
    

    You can also use nnz function to quickly know how many non-zeros you have

    nnz(distances)
    
    ans =
    
    14
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want build a sketch pad app on iPhone, I assume that this type
I want to build my own listView with selectedItem and Itemsource. I have started
I have an existing EJB3/Hibernate (backend) and JSP (frontend) website. I want build an
I have crazy problem with xcode 4 when ever I want build and run
Imagine that I have a matrix of 2x2 or 3x3 pictures and I want
I want build and run an enterprise APP that let me to manage my
I want to build an program that can detect square shape object in a
i want build a photography app with effects . e.g. old images with brown
i want to build a screen for user to get his marks on competitions,
I want to build flash application that can detect the user eyes color and

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.