-1

I recently started to refresh my python skills that I have been never really working on since I finished university 16 years ago. In my work life I have never been in contact with the python language. So I would like to embrace this language again. In order to do that I have found some problems that I am currently working on which I would like to finish soon:

Problem 1:

0.) Create a numpy array (6x6, randomly) with whole count numbers ranging from -10 to 10. After that complete the following matrix operations:

My idea:

import numpy as np M = np.random.randint(low=-10, high=10, size=(6, 6)) print(M)

a.) Cut the first row from the matrix

my idea:

first_row = M[0:1] print(first_row)

b.) Double the value of the elements from the 5th row by 2

my idea

5th_row = M[4:5] print(5th_row*2)

c.) Cut all odd columns (the sum of the column) from the 6x6 array

I heard that this can be done in one line. And I have now idea how to get the columns and display them as a matrix with the column_stack command...

d.) Cut a random 3x3 block from the 6x6 array

Again I cant even start with this one...

e.) Set all negative numbers in the 6x6 array to zero

I guess i can use if loops for each element, but i do have no idea how to filter the negative numbers from the positive ones and set the negative numbers zero

f.) Cut all even rows (the sum of the rows) from the 6x6 array

Again here I have big troubles tackling this problem...

Problem 2:

0.) I have a resonance curve as stated here:

A(eta,A_s,D)=A_s/(root[(1-eta²)²+(2etaD)²])

a.) For A_s = 1.0 I want to display a 2d-parametric plot with eta (x-axis) in a range between [0, 3] and the parameter D for [0.0.5,1.0,3.0]

It would be awesome if you could contribute some solutions to the mentioned problems.

best regards

Greta

  • 1
    Hi, guess that you should have a look at this course: https://www.kaggle.com/learn/python It is quick and with accompanying exercises. Guess that you should be able to review your problems more easily once you've covered it. – Alessio Jan 10 '21 at 12:35
  • @alessio Its good that you are the 3trd person who recommended me this awesome side. However I with my current level I am stil not able to solve a.)-f.) from problem one and problem 2 – Greta Stein Jan 10 '21 at 15:34

1 Answers1

2

below some directions for the questions of problem 1. You may test it on an online python ide such as repl.it

import numpy as np

mat = np.array([[-4, 3, 7, -9, 5, 2],[3, 1, -2, 7, 7, 1],
[2, 6, -4, 1, 5, -3],[2, 1, -2, -7, 1, -1],
[-5, 1, 4, 2, -3, 2],[7, 1, -3, -7, 4, -4]]
)

print('base matrix')
print(mat)

#1-a
print('Question #1-a')
first_row = mat[0:1]
print(first_row)

#1-b
print('Question #1-b')
fifth_row = mat[4]
print(fifth_row*2)

#1-c
print('Question #1-c')
remv_odd_cols = np.delete(mat, [0,2,4], 1)
print(remv_odd_cols)

#1-d
print('Question #1-d')
sub_mat = mat[1:4,1:4]
print(sub_mat)

#1-e
print('Question #1-e')
zeroed_mat = np.where(mat<0, 0, mat)
print(zeroed_mat)

#1-f
print('Question #1-f')
remv_even_rows = np.delete(mat, [1,3,5], 0)
print(remv_even_rows)

Concerning the problem 2, I think that a custom python function should be defined to calculate the eta values once a specific D value is set. Since eta is inside the function the previous eta value is used in the next calculation (at least that's what I understood for the written function!).

This will give you a plot. You can then replicate for the other D values.

The second problem is more complex, I guess you can begin by taking a look at problem one, and replicate it.

EDIT from the comment:

#select a random 6x6 matrix with items -10 / 10
import numpy as np
mat = np.random.randint(-10,10,(6,6))
print (mat)
#select a random int between 0 and 5
startIdx = np.random.randint(0,5)
print(startIdx)
#extracy submatrix (will be less than 3x3 id the index is out of bounds)
print(mat[startIdx:startIdx+3,startIdx:startIdx+3])
Alessio
  • 910
  • 7
  • 16
  • @ alesio Thank you for the hints for the problems. I just have a few questions: Q1[Problem_1, c.), d.), f.)]: I transformed your solution succesfully for any questions mentioned above except c, d and g. Therefore I would like to know how to solve this questions for a randomized matrix? – Greta Stein Jan 10 '21 at 19:08
  • 1
    Randomized, meaning different from a 6x6 or by taking each time a new 6x6 of integer with different values? – Alessio Jan 10 '21 at 20:03
  • Sorry for the possible poor description. I wanted to extract a random 3x3 block from a random 6x6 (e.g randomly scan one 3x3 grid from the 6x6 matrix and than plot it). – Greta Stein Jan 10 '21 at 20:24
  • 1
    just edited the main post, maybe this would help? :) – Alessio Jan 11 '21 at 00:07
  • solutions works perfectly for d.) Now I just need c.) and f.) – Greta Stein Jan 11 '21 at 08:09
  • Status Problem 2: I used your hints for this problem by defining a list which elements are extracted with a for-sequence: for D in [0.05, 1.0, 3.0]: def A(eta): return(function) +plot configuration(plt.xlabel, plt.ylabel, plt.grid, plt.title, np.linspace,...) – Greta Stein Jan 11 '21 at 08:15
  • This method plots a graph for each D a seperate graph. Is there a command which fuses all generated graphs in one diagramm? – Greta Stein Jan 11 '21 at 08:18
  • 1
    Maybe you can have a look at: https://stackoverflow.com/questions/22276066/how-to-plot-multiple-functions-on-the-same-figure-in-matplotlib `.plot` method should help stacking the plots you get for each `D` value – Alessio Jan 11 '21 at 20:41