Question

In python using numpy and for loops. How would I insert a multidimensional array into another...

In python using numpy and for loops. How would I insert a multidimensional array into another multidimensional array, multiple times. For example if I defined A = [[1,2],[3,4]] and B as a four by four matrix of zeros, How would I insert A into B such that when I print B I get, [[1,2,1,2],[3,4,3,4],[1,2,1,2],[3,4,3,4]]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is am going to use modulo operator on the index of A which will loop back on the array A and will help to get the pattern. you can use (i%n) & (i%m) in A(n*m) to get this pattern.

Code :

import numpy as np
A = np.array([[1,2],[3,4]]) // converting the list to arrray
B = np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]) // converting the list to arrray

for i in range(4):
for j in range(4):
B[i][j] = A[i%2][j%2] // assign the values of B

print(B)

Sample Run :

[[1 2 1 2]
[3 4 3 4]
[1 2 1 2]
[3 4 3 4]]

Add a comment
Know the answer?
Add Answer to:
In python using numpy and for loops. How would I insert a multidimensional array into another...
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
  • 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...

  • PYTHON: I have a numpy array that holds a name and a phone number such as...

    PYTHON: I have a numpy array that holds a name and a phone number such as this one. import numpy as np import pandas as pd LIST = np.array([("James", "Kirk", "512-375-5585"), ("Richard", "Branson, "1234567890"), ("James", "Bond", "34567")]) Then I stuff it into a data frame by doing this df = pd.DataFrame(List, columns=('Firstname', 'Lastname', 'Phone')) How would I go about changing 10 digit phone numbers such as 1234567890 into 123-456-7890 and discarding invalid ones such as 34567 so it reflects in...

  • D Question 12 1.5 pts Check the true statements about NumPy arrays: O A single instantiated NumPy array can store multi...

    D Question 12 1.5 pts Check the true statements about NumPy arrays: O A single instantiated NumPy array can store multiple types (e.g., ints and strings) in its individual element positions. A NumPy array object can be instantiated using multiple types (e.g., ints and strings) in the list passed to its constructor O Memory freeing will require a double-nested loop. The number of bits used to store a particular NumPy array object is fixed. O The numpy.append(my.array, new_list) operation mutates...

  • How would I use Numpy in python to search in arr2 with arr1 and return the...

    How would I use Numpy in python to search in arr2 with arr1 and return the string value. arr1=[0,a],[1,b],[2,c] arr2=[2],[1],[0] Expected outcome is =[c],[b],[a]

  • How to rewrite the following functions using only numpy array operations (universal functions, aggregations, boolean indexing)...

    How to rewrite the following functions using only numpy array operations (universal functions, aggregations, boolean indexing) without using loops def mean_squared_error (v , p ): n = len ( v [0]) result = 0 for i in range ( n ): result += ( v [0][ i ] - p [0])**2 + ( v [1][ i ] - p [1])**2 result = result / n return result I tried doing it like this but nothing printed out def mean_squared_error2 (v,...

  • I need a Python code for this problem. We can use python's array slicing in many...

    I need a Python code for this problem. We can use python's array slicing in many ways, and here is just one example. To take the forward derivative of an array y, we use (y[i+1] - y[i])/dx For example, if dx=1 , we might write a derivative routine as yderiv = zeros (len(y)-1) for i in range(len(y)-1): yderiv[i] = y(i+1] - y[i] Note that here, yderiv is one element shorter than y -- this is because you need 2 points...

  • I am able to make the array from 1 to 100, but unsure how to transform...

    I am able to make the array from 1 to 100, but unsure how to transform it to the listed matrices. It needs to be solved using numpy. Write a script to create an array a1, 2,,100]. Once the array is created transform it to the following matrix: 1 2 ...101 B-151 52 55 60 81 82... 90 91 92 100 Now, from the same array a create the following matrix: 2 12 C- 5 15 9 19 92 95...

  • How to copy the values of an array to another one that has a larger length...

    How to copy the values of an array to another one that has a larger length , without effecting it. For example: A = stdarray.create1D(5) A = [ 1,2,3,4,5] B = stdarray.create1D(10) B = [none, none, none, none, none, none, none , none, none, none] I want B to have A values.. B = [ 1,2,3,4,5, none, none, none, none, none] IN PYTHON, without using append.

  • Python Help: Using the stock.py data, write a program that does the following: Create a NumPy...

    Python Help: Using the stock.py data, write a program that does the following: Create a NumPy array from the nasdaq list from stocks.py. Do the same with the other three lists from stocks.py. Thus, at the end of this tasks, you will have four NumPy arrays. Print out the type (that is, the object type, not the element type) of each array in #1 and the data type typecode of the elements in each array in #1. This task is...

  • In python, how do you create an array of unknown dimensional size? I want to create...

    In python, how do you create an array of unknown dimensional size? I want to create an array of unknown dimensional size based on a number I recieve. To elaborate, let's say n = 2, it will output a 3D array {true, true, true}. If n = 3, then its output will be a 4D array {true, true, true, true}. Whatever the n value is, the return value will be an array of n + 1 dimensional size. How would...

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