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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:42:43+00:00 2026-06-17T23:42:43+00:00

I have a matrix m = zeros(1000, 1000) . Within this matrix I want

  • 0

I have a matrix m = zeros(1000, 1000). Within this matrix I want to draw an estimate of the line which passes through 2 points from my matrix. Let’s say x = [122 455]; and y = [500 500];.

How can I do this in Matlab? Are there any predefined functions to do this? I am using Matlab 2012b.

  • 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-17T23:42:45+00:00Added an answer on June 17, 2026 at 11:42 pm

    I’ll denote the two endpoints as p1 and p2 because I’m planning to use x and y for something else. I’m also assuming that the first coordinate of p1 and p2 is x and the second is y. So here’s a rather simple way to do it:

    1. Obtain the equation of the line y = ax + b. In MATLAB, this can be done by:

      x = p1(1):p2(1)
      dx = p2(1) - p1(1);
      dy = p2(2) - p1(2);
      y = round((x - p1(1)) * dy / dx + p1(2));
      
    2. Convert the values of x and y to indices of elements in the matrix, and set those elements to 1.

      idx = sub2ind(size(m), y, x);
      m(idx) = 1;
      

    Example

    Here’s an example for a small 10-by-10 matrix:

    %// This is our initial conditon
    m = zeros(10);
    p1 = [1, 4];
    p2 = [5, 7];
    
    %// Ensure the new x-dimension has the largest displacement
    [max_delta, ix] = max(abs(p2 - p1));
    iy = length(p1) - ix + 1;
    
    %// Draw a line from p1 to p2 on matrix m
    x = p1(ix):p2(ix);
    y = round((x - p1(ix)) * (p2(iy) - p1(iy)) / (p2(ix) - p1(ix)) + p1(iy));
    m(sub2ind(size(m), y, x)) = 1;
    m = shiftdim(m, ix > iy); %// Transpose result if necessary
    

    The result is:

    m =
         0     0     0     0     0     0     0     0     0     0
         0     0     0     0     0     0     0     0     0     0
         0     0     0     0     0     0     0     0     0     0
         1     0     0     0     0     0     0     0     0     0
         0     1     0     0     0     0     0     0     0     0
         0     0     1     1     0     0     0     0     0     0
         0     0     0     0     1     0     0     0     0     0
         0     0     0     0     0     0     0     0     0     0
         0     0     0     0     0     0     0     0     0     0
         0     0     0     0     0     0     0     0     0     0
    

    Update: I have patched this algorithm to work when dy > dx by treating the dimension with the largest displacement as if it were the x-dimension, and then transposing the result if necessary.

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

Sidebar

Related Questions

I have this code that creates a matrix filled with zeros. I want to
I have a non-fixed dimensional matrix M, from which I want to access a
I have a matrix A which I want to convert into a data.frame of
I have a 1x1 Matrix of points which specifies speed of a drive with
I want to build a square matrix. Let's suppose We have this matrix called
Say I have a matrix: from numpy import * a = zeros(shape=(nRows,nColumns)); and I
I have a NxN matrix which I want to split into non-overlap KxK block.
I have an NxN matrix filled with zeros. Now I want to add to
I have a matrix of objects that contains data in this form: name A,2,name
I have a m x n matrix where each row consists of zeros 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.