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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:13:56+00:00 2026-06-10T11:13:56+00:00

Dear stackoverflow users, Some years ago i used mathematica for several months. After not

  • 0

Dear stackoverflow users,

Some years ago i used mathematica for several months. After not programming for a few years I now do a research project, as a student, in which I use Matlab. I have found a lot of good help here on stackoverflow but now i am stuck at the following problem:

I have a data set of connections between nodes on a rectangular grid, each node has a possible connection to its 8 neighbors. My measurements are in the form of a 3 by n matrix where the first two values designate a node and the third value designates whether or not they are connected, the size of the grid is predetermined. Typically there are about ten lines coming from two or three nodes which are neighboring at least one of each other. The goal of my research project is to calculate the area at distance r around this collection of lines.

So far I have been able to plot the lines with the code below, for which I used bits of code from right here on stackoverflow, which was extremely useful. However I cant get a contour line around it at a certain distance (with which I would hope to calculate the area inside this contour line). The gplot function returns two vectors with two coordinates per line which I find difficult to convert to something more useable. I tried defining a value Z at a distance from the lines, to decline with distance from the lines, so I get a slope coming from these lines. From this slope i could calculate contourlines. However, because the lines are just coordinates I dont know how to calculate the distance to that line, opposed to when they would have been functions.

I am really at a loss. I hope I have somewhat clearly posted my problem here. This is the second time I post this problem, I have now added comments to the code and pictures to explain myself better. Thanks for any advice given!

This I have so far, the xls file is the 3 by n matrix i mention above, I have also written its contents in matrix form in the code below so my problem is easier to understand:

%# here i set my data/constants

filename='random.xls'; file=xlsread(filename); y=width; x=length; 

%# random.xls looks approximately like this, after xlsread(filename) you get

file=[21    22  1;
21  20  1;
15  16  1;
15  14  1;
15  23  1;
14  22  1;
14  21  1;
22  15  1;
23  14  1;
24  15  1;
6   15  1;
5   14  1;
7   14  1;
8   15  1];

%# predefined width and length, i usually get this from the file

width=8; length=4;

%# here i create my adjaceny matrix in a elegant way user amro posted on stackoverflow
%# however i immediately multiply it by 0, creating a y*x by y*x matrix with all zeroes

[X Y] = meshgrid(1:x,1:y); X = X(:); Y = Y(:); 
adjacency = squareform( pdist([X Y], 'chebychev') == 1 ); adjacency=adjacency*0;

%# here i take the matrix "file" for which the first two values are node numbers and 
%# the third value designates whether there is a connection between the two nodes to
%# fill in the connections in the adjacencymatrix

[nrows,ncols]=size(file);
for r = 1:nrows
      if file(r,3)==1
         adjacency(file(r,1),file(r,2))=1;
      end
end
adjacency=(adjacency+adjacency.');

%# plots the adjacencymatrix

subplot(121), spy(adjacency)

%# plots the connections and designates the nodes, note that the numbers designating
%# the nodes do not match original data, this is a separate problem i have not solved

[xx yy] = gplot(adjacency, [X Y]);
subplot(122), plot(xx, yy, 'ks-', 'MarkerFaceColor','b')

%# these last lines of code for plotting the numbers of the grid i do not fully
%# understand, in here is the cause for the numbers not matching the original data

axis([0 x+1 0 y+1])
[X Y] = meshgrid(1:x,1:y);
X = reshape(X',[],1) + 0.1; Y = reshape(Y',[],1) + 0.1;
text(X, Y(end:-1:1), cellstr(num2str((1:x*y)')) )
xlabel('length')
ylabel('width')
title(filename)

to clarify my problem i added these two pictures:
current plot https://i.stack.imgur.com/G1Qva.jpg
area i want to know https://i.stack.imgur.com/acnWf.jpg

  • 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-10T11:13:57+00:00Added an answer on June 10, 2026 at 11:13 am

    Solution to finding surface area inside isoline or contourline at distance r from collection of lines in Matlab, approximation by graphical processing (dilating), not an exact or efficient awnser!

    I have made an approximation, so this is not an exact awnser nor is it efficient coding. A friend of mine who studies machine vision suggested converting the lines to pixels and then dilating the image with a disk, after which the pixel count is a measure of surface area:

    %# constants and variables
    minx = min(xx);
    miny = min(yy);
    maxx = max(xx);
    maxy = max(yy);
    rangex = maxx - minx;
    rangey = maxy - miny;
    borderRelNum = sqrt(2);
    electrodeToImageScaleFactor = 100;
    imsizex = 2*(maxx+borderRelNum)*electrodeToImageScaleFactor+2;
    imsizey = 2*(maxy+borderRelNum)*electrodeToImageScaleFactor+2;
    im = zeros(imsizex, imsizey);
    grayscalevalue = 255;
    disksize = round(borderRelNum*electrodeToImageScaleFactor);
    
    %# transformation matrices
    centerElectrodeSpace = [1, 0, -(minx + maxx) / 2;
                    0, 1, -(miny + maxy) / 2;
                    0, 0, 1 ];
    
    scaleElectrodeToImage = [electrodeToImageScaleFactor , 0, 0;
                 0, electrodeToImageScaleFactor , 0;
                 0, 0, 1 ];
    
    centerImageSpace = [ 1, 0, imsizex / 2;
                 0, 1, imsizey / 2;
                 0, 0, 1 ];
    
    electrodeToImage = centerImageSpace * scaleElectrodeToImage * centerElectrodeSpace;
    
    %# transformation
    
    for i = 0:(size(xx,1) / 3 - 1) 
        p1 = [xx(i*3 + 1); yy(i*3 + 1); 1];
        p2 = [xx(i*3 + 2); yy(i*3 + 2); 1];     
    
        p1im = electrodeToImage * p1
        p2im = electrodeToImage * p2
    
        lx = linspace( min( p1im(1), p2im(1) ), max( p1im(1), p2im(1) ), borderRelNum*electrodeToImageScaleFactor )
        ly = linspace( min( p1im(2), p2im(2) ), max( p1im(2), p2im(2) ), borderRelNum*electrodeToImageScaleFactor )
    
        index = sub2ind(size(im),round(lx),round(ly));
        im(index) = grayscalevalue;     
    end
    
    %# Now dilate and count pixels
        se = strel('disk', disksize, 0);
    im = imdilate(im, se);
    image(im)
        colormap(gray)
        sum(sum(im/grayscalevalue))*(1/electrodeToImageScaleFactor^2)
    

    If someone is able to solve my problem more elegantly, efficiently or more precisely i would still very much appreciate it. But this will do for now.

    -edit- ok this is VERY inefficient indeed, my pc has been crunching numbers for 30 minutes on my data set (10 xls files, not that much) now and is still at file 1 it seems if i look at workspace values

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

Sidebar

Related Questions

Hello dear users of the stackoverflow! I really like the search form on this
Dear visitors stackoverflow! I can not solve a simple problem: Body given the background
Dear stackoverflow members, I have a small problem, I want to replace some characters
Dear StackOverflow users, I found a of information about what Drawables to use (HDPI,
dear all.i newbie at web programming and until now i still have learn about
Dear stackoverflow users, I'm currently working on a schoolproject which involves generating a PHP
Hallo Dear Stackoverflow users. Working with sql on sql server I have searched the
Dear Friends from Stackoverflow, Please help me with a problem that i'm having when
Dear members of the Stackoverflow community, We are developing a web application using the
Dear all, I have a question about Facebook Page: ( NOT user profile page,

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.