基于指定角度提取二维数组中目标元素所在直线上的元素索引
Let's break this problem down into practical, actionable steps to get the result you need:
- Locate all positions of the value 149 in the 10x10 array.
- Randomly pick one of these positions as our starting point.
- Translate the specified angle into a direction step (how row and column values change with each move along the angle).
- Generate 5 valid indices along that direction, making sure we don't go outside the array bounds.
- Pull the corresponding values for those indices.
Step-by-Step Implementation (Python)
We'll use NumPy for array handling and the random module to select our starting index.
1. Define the Array and Find Target Indices
First, let's set up the array and find every spot where the value is 149:
import numpy as np import random # Your 10x10 array A = np.array([[ 50, 385, 67, 445, 336, 305, 48, 419, 106, 39], [217, 188, 139, 64, 258, 340, 188, 18, 58, 278], [201, 43, 457, 196, 149, 289, 350, 86, 495, 448], [379, 376, 217, 124, 264, 69, 378, 296, 200, 416], [234, 65, 420, 44, 489, 451, 46, 216, 136, 97], [470, 162, 183, 480, 149, 482, 456, 294, 89, 337], [164, 335, 443, 305, 30, 381, 341, 331, 149, 261], [389, 160, 448, 304, 30, 280, 333, 360, 166, 300], [ 63, 346, 321, 229, 432, 129, 100, 217, 83, 196], [218, 33, 430, 237, 225, 391, 393, 344, 457, 82]]) # Get all (row, column) pairs where the value equals 149 target_indices = list(zip(*np.where(A == 149))) print("All positions of 149:", target_indices) # Output: [(2,4), (5,4), (6,8)]
2. Randomly Select a Starting Index
Next, we'll pick a random starting point from our list of 149 positions:
# Choose a random starting index start_r, start_c = random.choice(target_indices) print(f"Randomly selected start index: ({start_r}, {start_c})")
3. Map Angles to Direction Steps
We need to convert each supported angle into a step change for rows (dr) and columns (dc). Here's how we define the directions (rows increase downward, columns increase rightward):
- 0°: Right (no row change, column +1)
- 45°: Up-Right (row -1, column +1)
- 90°: Up (row -1, no column change)
- 135°: Up-Left (row -1, column -1)
- 180°: Left (no row change, column -1)
- -135°: Down-Left (row +1, column -1)
- -90°: Down (row +1, no column change)
- -45°: Down-Right (row +1, column +1)
# Dictionary to map angles to their (row_step, column_step) pairs angle_to_step = { 0: (0, 1), 45: (-1, 1), 90: (-1, 0), 135: (-1, -1), 180: (0, -1), -135: (1, -1), -90: (1, 0), -45: (1, 1) } # Specify your desired angle (e.g., 45 degrees as in your example) desired_angle = 45 dr, dc = angle_to_step[desired_angle]
4. Generate Indices Along the Angle
We'll create 5 indices (including the starting point) and validate each one to ensure it stays within the array bounds:
result_indices = [] for i in range(5): current_r = start_r + i * dr current_c = start_c + i * dc # Check if the index is within the 10x10 array if 0 <= current_r < A.shape[0] and 0 <= current_c < A.shape[1]: result_indices.append((current_r, current_c)) else: print(f"Index ({current_r}, {current_c}) is out of bounds — stopping early.") break
5. Retrieve the Corresponding Values
Finally, let's get the values for our generated indices:
# Extract values from the array using our result indices result_values = [A[r][c] for r, c in result_indices] print("\nFinal Result:") print(f"Indices: {result_indices}") print(f"Values: {result_values}")
Example Output
If the random starting index is (5,4) (value 149) and the angle is 45°, you'll get exactly the example you provided:
Indices: [(5, 4), (4, 5), (3, 6), (2, 7), (1, 8)] Values: [149, 451, 378, 86, 58]
Notes
- If any step goes outside the array bounds, the code stops early and returns only valid indices. You can adjust this behavior (like wrapping around or padding with a default value) if needed.
- The angle mapping follows standard directional conventions relative to how arrays are structured (rows down, columns right).
内容的提问来源于stack exchange,提问作者Yusuf Falola

