Taking a slice of a two dimensional array

  • Thread starter jing2178
  • Start date
  • Tags
    Array
In summary, The conversation is about a question regarding a two dimensional array of numbers and how to form a one-dimensional slice of the array by drawing a line. The person asking the question is wondering if there are already recognized ways of doing this and if there are better ways than the one they have tried. They also ask if the question can be stated more succinctly. The other person suggests three possible methods and mentions the Bresenham's line algorithm as an example. The conversation ends with the person thanking the other for their input and mentioning they will need to consider the projection of the line on boundary lines parallel to the axes.
  • #1
jing2178
41
2
The source of this question is an unanswered question at stackoverflow but I believe is more mathematical than computational.

My question arising from this is.

You have a two dimensional array of numbers, each element of the array is represented by a square box. The boxes arranged into a rectangle with element (i,j) represented by the box in position (i,j). A 1-dimensional slice of the array is formed by drawing a line across the boxes, choosing boxes based on the position of the line crossing them and a vector formed on the basis of the chosen boxes.

This pdf file shows an issue arising following one attempt to do this.

Are there already recognised ways of doing this?
Are there better ways of doing this than the way I have tried?
Can the question be stated in a more succinct way?

Any help please.
 
Mathematics news on Phys.org
  • #2
All 3 methods are possible - and you could take alternating lower/upper pixel as well if "combined pixel" is not practical for some reason.
To make things even more complicated: The cell right of 2 is not hit in the center - you could try to take that into account as well.

Which method is best for you depends on the application.
 
  • #4
NemoReally said:

Thank you that is useful. As it is still a 2D representation of the line I think I will need to look at the projection of the line (and hence pixels) on boundary lines parallel to the axes.
 
  • #5


I would approach this question by first clarifying the specific goal or purpose of taking a slice of a two-dimensional array. Is it to extract specific data points for further analysis? Is it for visualization purposes? Answering these questions can help determine the most appropriate method for taking the slice.

Next, I would explore existing literature and research on this topic to see if there are already established methods or algorithms for taking slices of two-dimensional arrays. It is possible that there are recognized ways of doing this that have been extensively studied and tested.

If there are no established methods or if the existing methods do not suit the specific needs of the question, then I would suggest experimenting with different approaches and comparing their effectiveness. This could involve varying the position and direction of the slicing line or using different criteria for choosing which boxes to include in the slice.

In terms of stating the question more succinctly, it may be helpful to clearly define the specific parameters and goals of the slice, such as the dimensions of the array, the position of the slicing line, and the desired output. This can help focus the research and provide a more concise question to be answered.

Overall, I would recommend further research and experimentation to find the most effective and efficient way to take a slice of a two-dimensional array for the specific purpose in question.
 

Related to Taking a slice of a two dimensional array

1. How do you take a slice of a two dimensional array?

To take a slice of a two dimensional array, you can use the built-in method slice(). This method allows you to specify the starting and ending indices of the slice you want to extract from the array.

2. What is the syntax for taking a slice of a two dimensional array?

The syntax for taking a slice of a two dimensional array is array.slice(startIndex, endIndex). The startIndex is the index where the slice should start and the endIndex is the index where the slice should end (not including the element at the endIndex).

3. Can you take a slice of a two dimensional array without modifying the original array?

Yes, you can take a slice of a two dimensional array without modifying the original array by using the slice() method. This method returns a new array with the elements from the specified slice, leaving the original array unchanged.

4. How do you take a slice of a specific row or column from a two dimensional array?

To take a slice of a specific row or column from a two dimensional array, you can use the map() method in combination with the slice() method. For a row, you can use the map() method to return a new array with the specified row, and for a column, you can use the map() method to loop through each row and return the element at the specified index for each row.

5. What happens if the specified indices for the slice are out of range?

If the specified indices for the slice are out of range (i.e. the startIndex is greater than the endIndex or either index is larger than the length of the array), the slice() method will return an empty array.

Similar threads

Replies
21
Views
2K
Replies
2
Views
1K
Replies
4
Views
3K
Replies
2
Views
868
Replies
3
Views
2K
Replies
4
Views
6K
Replies
14
Views
4K
Back
Top