Question

can you please create the code program in PYTHON for me using numpy. i want to...

can you please create the code program in PYTHON for me using numpy.

i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this:

if N = 16, matrix is
[ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4]

if N = 64, matrix is
[8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8 8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8 8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8 8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8]

if N = 256, matrix is
[16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -16 -6 -16 -16 -16 -16 -16]

and so on for N = 1024, 4096, etc

please paste the code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code:

'''
To create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of
elements like this:

if N = 16, matrix is
[ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4]

if N = 64, matrix is
[8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8 8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8
8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8 8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8 -8]
'''  
import numpy as np

def gen_matrix(N):
   # assumed that N is a multiple of 4
   sqrt_N = int(np.sqrt(N)) # type of np.sqrt(N) is np.float64 so converting to int for further use
   final_array = np.array([])
   final_array = final_array.astype(int) # converting the array type from np.float64 to int

   # iterating to fill up the final_array
   for i in range(sqrt_N):
       temp = ((-1)**i)*sqrt_N # calculating the value of the array elements
       temp_list = [temp]*sqrt_N # creating an array of sqrt_N elems each having value=temp
       final_array = np.append(final_array,temp_list) # appending the above created array into the final array

   return final_array

if __name__ == '__main__':
   print(gen_matrix(64))

Code Snapshot:

Result:

Add a comment
Know the answer?
Add Answer to:
can you please create the code program in PYTHON for me using numpy. i want to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python please create a code that #Use NumPy and MatPlotLib to create a plot of...

    Using Python please create a code that #Use NumPy and MatPlotLib to create a plot of f(x) = x**4 + 10 # in the range x=(-10, 10) (inclusive), with a point on the plot every 0.5 change in x. # Include the correct import statements

  • python Problem 3-6 points First, start with the following code: import numpy as np A np.random....

    python Problem 3-6 points First, start with the following code: import numpy as np A np.random. randint (0, 10, sie n,n)) np. savetxt ('exam2.txt', A, fmt-idelimiter B-# np. zeros ( (n, n) , dtypes, int 64, ) When run, it will produce a file named "exam2.txt" that has 5 rows each with 5 numbers separated by commas,. In addition, a 5 by 5 array of zeros named B is defined. Run this code, but do not change it Your job...

  • need help with this python progam using numpy Create a 4x4 two dimensional array with numbers...

    need help with this python progam using numpy Create a 4x4 two dimensional array with numbers from 1 thru 16. show this then: Change the last element by dividing it in half. Show that the original array has changed. show this then: Set all values in row 2 to zero. Show the original array contains this change show this then: Set all values in column 1 to one. Shoe the original array contains this change. show this then: Display the...

  • (COs 1 and 8) Create a Python program that calculates the tip and total for a...

    (COs 1 and 8) Create a Python program that calculates the tip and total for a meal at a restaurant. Paste Python code here; output not required. Sample output: Tip Calculator Enter cost of meal: 52.31 Enter tip percent: 20 Tip amount: 10.46 Total amount: 62.77 Specifications: •Input: costOfMeal, tipPercent         The formula for calculating the tip amount is: tip = costOfMeal * (tipPercent / 100) •         The program should accept decimal entries like 52.31 and 15.5. •         Assume the user...

  • code that I started: # THIS IS NEEDED TO USE ARRAYS IN PYTHON: from array import * # THIS IS NEEDED TO MAKE PLOTS IN PY...

    code that I started: # THIS IS NEEDED TO USE ARRAYS IN PYTHON: from array import * # THIS IS NEEDED TO MAKE PLOTS IN PYTHON: import matplotlib.pyplot as plt # IMPORT A ROUTINE FROM NUMPY USEFUL FOR CREATING AN # ARRAY FILLED WITH ZEROS from numpy import zeros # THIS COMMAND CREATES AN ARRAY OF ZEROS WITH DESIRED SHAPE: V=zeros([31,21]) newV=zeros([31,21]) # SET UP THE BOUNDARY VALUES: V=8 ON TOP OF BOX iy=20 for ix in range(0,31): V[ix,iy]=8.0 #...

  • Provide Python code that does the following: Create the following list in your program: lst1 =...

    Provide Python code that does the following: Create the following list in your program: lst1 = ["apple", "banana", "pear", "grapefruit", "pineapple", "grape", "guava", "plum", "peach"] Write code that then does the following: Print the last item in the list. Print the first item in the list. Print all but the last three items in the list. Print the first, third, fifth etc. items of the list. Print the second, fourth, sixth etc. items of the list. Parts A- E should...

  • Project 5 - intro to python Write a program in python that calculates the amount of...

    Project 5 - intro to python Write a program in python that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, four pennies the third day, and continues to double each day. Output the amount earned each day .. and the total pay at the end. The Algorithm (Plan) for your program would be: Ask the user for the...

  • provide a python script please For the following vectors and matrices, using the NumPy library, write...

    provide a python script please For the following vectors and matrices, using the NumPy library, write a script file that determines the solution for x given by the equation x-(A AT)b-Cd where AT denotes the transpose of A 1 2 3 4 16 12 8 4 A-2 4 6 8 3 6 9 12 4 8 12 16 C-12 9 6 3 8 6 4 2 4 3 2 1 4 The output to the command terminal prompt within Spyder...

  • The following are short Python calculations. Give the answer and the Python code for each. 1....

    The following are short Python calculations. Give the answer and the Python code for each. 1. What is the sum of the numbers from 22:100 incrementing by 2? Do this in two ways: Use a while statement and a for loop to do the calculation. (Hint: make sure the numbers include 100) 2. What is the mean, standard deviation of the square root of the numbers from 3 to 5 incrementing by 0.1? Use the linspace function from the numpy...

  • Please Help me to create Morse code program on python3 by using tkinter ?

    Please Help me to create Morse code program on python3 by using tkinter ?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT