22053686-Ad Lab-3-0.5
22053686-Ad Lab-3-0.5
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:
Source code
import numpy as np
vector = np.zeros(25)
vector[4:10] = 1
print(vector)
Program Title: Write a program to create a vector with values ranging from 5 to 20
& reverse it
Input/Output Screenshots:
Page 1
Applications Development Laboratory (CS33002), Spring 202
Source code
import numpy as np
print(reversed_vector)
Program Title: Write a program to create a vector with 10 random values & sort
first half ascending & second half descending.
Input/Output Screenshots:
Source code
import numpy as np
vector = np.random.random(10)
first_half_sorted = np.sort(vector[:5])
second_half_sorted = np.sort(vector[5:])[::-1]
print(sorted_vector)
Program Title: Write a program to create a vector of size 10 with values ranging from
0 to 1, both excluded.
Input/Output Screenshots:
Page 2
Applications Development Laboratory (CS33002), Spring 202
Source code
import numpy as np
print(vector)
Input/Output Screenshots:
Source code
import numpy as np
print(concatenated_array)
Program Title: Write a program to create a 4x3 array with random values & find
out the minimum & maximum values.
Page 3
Applications Development Laboratory (CS33002), Spring 202
Input/Output Screenshots:
Source code
import numpy as np
min_value = np.min(array)
max_value = np.max(array)
print("Array:")
print(array)
print("\nMinimum value:", min_value)
print("Maximum value:", max_value)
Program Title: Write a program to create a 2D array with 1 on the border and 0 inside.
Input/Output Screenshots:
Page 4
Applications Development Laboratory (CS33002), Spring 202
Source code
import numpy as np
array[0, :] = 1
array[-1, :] = 1
array[:, 0] = 1
array[:, -1] = 1
return array
rows, cols = 5, 5
Program Title: Write a program to create a 4x4 matrix with values 1,2,3,4 just below
& above the diagonal, rest zeros.
Input/Output Screenshots:
Page 5
Applications Development Laboratory (CS33002), Spring 202
Source code
import numpy as np
print(matrix)
Program Title: Write a program to extract all the contiguous 3x3 blocks from a random
10x10 matrix.
Input/Output Screenshots:
Page 6
Applications Development Laboratory (CS33002), Spring 202
Source code
import numpy as np
blocks = []
for i in range(matrix.shape[0] - 2):
for j in range(matrix.shape[1] - 2):
block = matrix[i:i+3, j:j+3]
blocks.append(block)
Program Title: A magic square is a matrix all of whose row sums, column sums and the
sums of the two diagonals are 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
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]])
Page 7
Applications Development Laboratory (CS33002), Spring 202
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:
Page 8
Applications Development Laboratory (CS33002), Spring 202
Source code
import numpy as np
main_diagonal_sum = np.sum(np.diag(A))
anti_diagonal_sum = np.sum(np.diag(np.fliplr(A)))
max_sum = np.max(all_sums)
min_sum = np.min(all_sums)
if max_sum == min_sum:
print("\nThe matrix is a magic square.")
else:
print("\nThe matrix is not a magic square.")
Page 9