-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

2
  • 1
    Hi, guess that you should have a look at this course: 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. Commented Jan 10, 2021 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 Commented Jan 10, 2021 at 15:34

1 Answer 1

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])
Sign up to request clarification or add additional context in comments.

8 Comments

@ 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?
Randomized, meaning different from a 6x6 or by taking each time a new 6x6 of integer with different values?
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).
just edited the main post, maybe this would help? :)
Maybe you can have a look at: stackoverflow.com/questions/22276066/… .plot method should help stacking the plots you get for each D value
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.