3 SC
3 SC
Program Title:Write a program to create a null vector of size 25 but the values from fifth
to tenth elements are all 1.
Input/Output Screenshots:
RUN-1:
Source code
import numpy as np
vector = np.zeros(25)
vector[4:10] = 1
print(vector)
Page
T&T Lab.(CS-3096), Spring 2023
Program Title:Write a program to create a vector with values ranging from 5 to 20 &
reverse it
Input/Output Screenshots:
RUN-1:
Source code
import numpy as np
vector_reversed = vector[::-1]
Program No:6.3
Page
T&T Lab.(CS-3096), Spring 2023
Program Title:Write a program to create a vector with 10 random values & sort first half
ascending & second half descending.
Input/Output Screenshots:
RUN-1:
Source code
import numpy as np
vector = np.random.rand(10)
vector[:len(vector)//2] = np.sort(vector[:len(vector)//2])
vector[len(vector)//2:] = np.sort(vector[len(vector)//2:])[::-1]
Program No:6.4
Page
T&T Lab.(CS-3096), Spring 2023
Program Title:Write a program to create a vector of size 10 with values ranging from 0
to 1, both excluded.
Input/Output Screenshots:
RUN-1:
Source code
import numpy as np
print(vector)
Program No:6.5
Input/Output Screenshots:
RUN-1:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
import numpy as np
print(concatenated)
Program Title:Write a program to create a 4x3 array with random values & find out the
minimum & maximum values.
Input/Output Screenshots:
RUN-1:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
import numpy as np
array = np.random.rand(4, 3)
min_value = np.min(array)
max_value = np.max(array)
print("Array:\n", array)
Program No:6.7
Program Title:Write a program to create a 2D array with 1 on the border and 0 inside.
Input/Output Screenshots:
RUN-1:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
import numpy as np
array[0,:] = 1
array[-1,:] = 1
array[:,0] = 1
array[:,-1] = 1
print(array)
Program Title:Write a program to create a 4x4 matrix with values 1,2,3,4 just below &
above the diagonal, rest zeros.
Page
T&T Lab.(CS-3096), Spring 2023
Input/Output Screenshots:
RUN-1:
Source code
import numpy as np
print(matrix)
Program No:6.9
Program Title:Write a program to extract all the contiguous 3x3 blocks from a random
10x10 matrix
Input/Output Screenshots:
RUN-1:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
import numpy as np
for i in range(blocks.shape[0]):
for j in range(blocks.shape[1]):
print(blocks[i][j])
Program No:6.10
Program Title:A magic square is a matrix all of whose row sums, column sums and the
sums of the two diagonals are
Page
T&T Lab.(CS-3096), Spring 2023
the same. (One diagonal of a matrix goes from the top left to the bottom right, the other
diagonal goes
from top right to bottom left.) Show by direct computation that if the matrix A is given by
The matrix A has 5 row sums (one for each row), 5 column sums (one for each column)
and two
diagonal sums. These 12 sums should all be exactly the same, and you could verify that
they are the
same by printing them and “seeing” that they are the same. It is easy to miss small
differences among so
many numbers, though. Instead, verify that A is a magic square by constructing the 5
column sums and
computing the maximum and minimum values of the column sums. Do the same for the 5
row sums,
and compute the two diagonal sums. Check that these six values are the same. If the
maximum and
minimum values are the same, the flyswatter principle says that all values are the same.
Hints: The function np.diag extracts the diagonal of a matrix, and the function np.fliplr
extracts the other
diagonal
Input/Output Screenshots:
RUN-1:
Page
T&T Lab.(CS-3096), Spring 2023
Source code
import numpy as np
A = np.array([[17, 24, 1, 8, 15], [23, 5, 7, 14, 16], [ 4, 6, 13, 20, 22], [10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]])
diag1_sum = np.sum(np.diag(A))
diag2_sum = np.sum(np.diag(np.fliplr(A)))
Page
T&T Lab.(CS-3096), Spring 2023
if np.allclose(sums, sums[0]):
else:
Page