Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi~

I got a problem about 2 Dimension Array.

If I got a 5 times 5 matrix. It contains only 0 and 1 elements.

Can I get the x,y coordinates for element 1 location?

Matrix =
{ 0, 0, 1, 0, 0}
{ 0, 0, 0, 0, 0}
{ 1, 0, 0, 0, 1}
{ 0, 0, 0, 0, 0}
{ 0, 0, 1, 0, 0}

which
Array(0,2)=1 (x coordinate most left)
Array(4,2)=1 (x coordinate most right)
Array(2,0)=1 (y coordinate most top)
Array(2,4)=1 (y coordinate most bottom)

What I have tried:

C#
for (int x = 0; x < 5; x++)
{
     for (int y = 0; y < 5; y++)
     {
          if (Array[x, y] == 1)
          {
              int Xo = x;
              int Yo = y;
              textBox1.Text = Xo.ToString();
              textBox2.Text = Yo.ToString();
          }
     }
}

           for (int x = 5; x > 0; x--)
            {
                for (int y = 5; y > 0; y--)
                {
                    if (Array[x, y] == 1)
                    {
                        int Xf = x;
                        int Yf = y;
                        textBox3.Text = Xf.ToString();
                        textBox4.Text = Yf.ToString();
                    }
                }
            }
Posted
Updated 27-Jan-21 13:06pm
v4

Quote:
Can I get the x,y coordinates for element 1 location?
Yes.

But not with that code.
Your textboxes contain only one item: so each time you go round the loop and find a new "1" location, you overwrite and throw away the previous detection.
So your boxes always show the last such location found, and can never display more than that.

To display multiple locations, you would need to change to a different control (one that can show multiple values, such as a DataGridView perhaps) or change from two textboxes to a multiline text box which shows them as pairs on separate lines.
 
Share this answer
 
Comments
Jacky CWT 26-Jan-21 19:25pm    
Dear Original Griff:

Thanks your reply.

I probably got your idea. You mean that I can use "database" to save all the x, y coordinate then use "select * from database where....."

But In my case, I only want to get the most Top/Bottom/left/Right location.

For my code

First loop can find the most Right "1" location.
Second loop can find the most Left "1" location.

As you said in your answer.

How can I get the most Top and most Bottom location?

just switch the Outer/inner loop?
OriginalGriff 27-Jan-21 4:41am    
Use two loops: one goes from the top left and moves right and down; the other goes from the bottom right, and moves left and up.

No point in looking in the middle is you only want the extremes!
Jacky CWT 28-Jan-21 19:58pm    
Dear OriginalGriff

Yes, I am trying to detect extremes of matrix location.

If you create your Matrix
C#
int[,] matrix =
{
    { 0, 0, 1, 0, 0},
    { 0, 0, 0, 0, 0},
    { 1, 0, 0, 0, 1},
    { 0, 0, 0, 0, 0},
    { 0, 0, 1, 0, 0}
};
you will see by using a debugger
that the coordinates of the '1 locations' are not what you expect.

The coordinates are
[0,2]
[2,0]
[2,4]
[4,2]
In order to get these positions you could loop through the matrix and store the items in a list.
C#
public class Item
{
	public int X { get; set; }
	public int Y { get; set; }
	public int Value { get; set; }
}
C#
var items = new List();

for (var x = 0; x < 5; x++)
{
	for (var y = 0; y < 5; y++)
	{
		items.Add(new Item
		{
			X = x,
			Y = y,
			Value = matrix[x, y]
		});
	}
}
The searched positions can be retrieved like this:
C#
var results = items.Where(x => x.Value == 1);
 
Share this answer
 
Comments
Jacky CWT 1-Feb-21 3:19am    
Thank you SteveJudge.

^_^
TheRealSteveJudge 1-Feb-21 3:27am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900