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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:43:53+00:00 2026-06-18T02:43:53+00:00

I have a 6 * 6 matrix A= 3 8 8 8 8 8

  • 0

I have a 6 * 6 matrix

A=
  3     8     8     8     8     8
  4     6     1     0     7    -1
  9     7     0     2     6    -1
  7     0     0     5     4     4
  4    -1     0     2     8     1
  1    -1     0     8     3     9

I am interested in finding row and column number of neighbors starting from A(4,4)=5. But They will be linked to A(4,4) as neighbor only if A(4,4) has element 4 on right, 6 on left, 2 on top, 8 on bottom 1 on top left diagonally, 3 on top right diagonally, 7 on bottom left diagonally and 9 on bottom right diagonally. TO be more clear A(4,4) will have neighbors if the neighbors are surrounding A(4,4) as follows:

 1     2     3;
 6     5     4;
 7     8     9;

And this will continue as each neighbor is found.
Also 0 and -1 will be ignored. In the end I want to have these cells’ row and column number as shown in figure below. Is there any way to visualize this network as well. This is sample only. I really have a huge matrix.

enter image description here

  • 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-18T02:43:54+00:00Added an answer on June 18, 2026 at 2:43 am
    A = [3     8     8     8     8     8;
         4     6     1     0     7    -1;
         9     7     0     2     6    -1;
         7     0     0     5     4     4;
         4    -1     0     2     8     1;
         1    -1     0     8     3     9];
    
    test = [1 2 3; 
            6 5 4; 
            7 8 9];
    
    %//Pad A with zeros on each side so that comparing with test never overruns the boundries    
    %//BTW if you have the image processing toolbox you can use the padarray() function to handle this
    P = zeros(size(A) + 2);        
    P(2:end-1, 2:end-1) = A;
    
    current = zeros(size(A) + 2);
    past = zeros(size(A) + 2);
    
    %//Initial state (starting point)
    current(5,5) = 1; %//This is A(4,4) but shifted up 1 because of the padding
    
    condition = 1;
    
    while sum(condition(:)) > 0;
          %//get the coordinates of any new values added to current
         [x, y] = find(current - past); 
         %//update past to last iterations current
         past = current; 
    
         %//loop through all the coordinates returned by find above
         for ii=1:size(x); 
    
             %//Make coord vectors that represent the current coordinate plus it 8 immediate neighbours.
             %//Note that this is why we padded the side in the beginning, so if we hit a coordinate on an edge, we can still get 8 neighbours for it!
             xcoords = x(ii)-1:x(ii)+1; 
             ycoords = y(ii)-1:y(ii)+1; 
    
             %//Update current based on comparing the coord and its neighbours against the test matrix, be sure to keep the past found points hence the OR
             current(xcoords, ycoords) = (P(xcoords, ycoords) == test) | current(xcoords, ycoords); 
    
         end 
    
         %//The stopping condition is when current == past
         condition = current - past;
    
     end 
    
    %//Strip off the padded sides
    FinalAnswer = current(2:end-1, 2:end-1)
    [R, C] = find(FinalAnswer);
    coords = [R C] %//This line is unnecessary, it just prints out the results at the end for you.
    

    OK cool you got very close, so here is the final solution with the loops. It runs in about 0.002 seconds so it’s pretty quick I think. The output is

    FinalAnswer =
    
         0     0     0     0     0     0
         0     1     1     0     0     0
         0     1     0     1     0     0
         1     0     0     1     1     1
         0     0     0     0     1     0
         0     0     0     0     0     1
    
    
    coords =
    
         4     1
         2     2
         3     2
         2     3
         3     4
         4     4
         4     5
         5     5
         4     6
         6     6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix X(1e4,20) which takes on values 0:4 . I'm interested in
I have matrix (a) with (1:10),<10 x 1> double. I would like to copy
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix of data (the columns represent time, and the rows spectrum
I have the matrix m <- matrix(c(1, 0, 3, 4, 0, 6), 3) I
I have a matrix A which I want to convert into a data.frame of
I have a matrix of objects that contains data in this form: name A,2,name
I have a matrix in SAS/IML: x = {7 6 3 3 8, 2
I have a matrix in the type of a Numpy array. How would I
I have the matrix system: A x B = C A is a by

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.