make a code . where you take even and odd column of a matrix . there from even column…
- User
make a code .
where you take even and odd column of a matrix . there from even column take even number from top to bottom and print it in a line. in next line for the odd column take odd number from bottom to top and print it in next line . do it similarly for other column
input :
floor = np.array([[ '3' , '8' , '4' , '6' , '1'], ['7' , '2' , '1' , '9' , '3'], ['9' , '0' , '7' , '5' , '8'], ['2' , '1' , '3' , '4' , '0']] )
Output will be : 3 9 1 1 2 4 7 2 4 9 1 8 6
- Assistant
To achieve the desired output, you'll need to iterate through the columns of the given matrix, extract even numbers from even-indexed columns (in a top-to-bottom order), and extract odd numbers from odd-indexed columns (in a bottom-to-top order). Here's how you can implement this logic in Python using NumPy:
import numpy as np # Input matrix floor = np.array([ ['3', '8', '4', '6', '1'], ['7', '2', '1', '9', '3'], ['9', '0', '7', '5', '8'], ['2', '1', '3', '4', '0'] ]) # Determine the number of columns num_cols = floor.shape[1] # Iterate over each column for col in range(num_cols): if col % 2 == 0: # Even column index # Extract even numbers from top to bottom even_numbers = [floor[row][col] for row in range(floor.shape[0]) if int(floor[row][col]) % 2 == 0] print(' '.join(even_numbers)) else: # Odd column index # Extract odd numbers from bottom to top odd_numbers = [floor[row][col] for row in range(floor.shape[0]-1, -1, -1) if int(floor[row][col]) % 2 == 1] print(' '.join(odd_numbers))