Bottom Page

• 2 Vote(s) - 1.5 Average
• 1
• 2
• 3
• 4
• 5
 Finding Row Number for Items in 2D array fafzal Unladen Swallow Posts: 2 Threads: 1 Joined: Jan 2019 Reputation: 0 Likes received: 0 #1 Jan-10-2019, 05:33 AM (This post was last modified: Jan-10-2019, 05:33 AM by fafzal. Edited 1 time in total.) Hi everyone. First post here because I'm completely stuck on my homework assignment. I don't have a strong coding background so any help is greatly appreciated. I have a set of 25 randomly generated numbers. The array is shaped 5 x 5. I need to be able to multiply the numbers in each row with it's row numbers. All numbers in row 1 x 1, all numbers in row 2 x 2 etc. Here is what I have and I'm not sure how to get the address (row number) for every item in the array: ```import random mu, stdev = 5, 5 random.seed(1) three = np.random.normal(mu, stdev, 25).reshape(5,5) for x in three: y = x * x.shape[0] print (x.shape[0])``` scidam Minister of Silly Walks Posts: 354 Threads: 0 Joined: Mar 2018 Reputation: 49 Likes received: 53 #2 Jan-10-2019, 05:57 AM No loops needed. Numpy can do all the work by itself: ```three = three * np.arange(three.shape[0])[:, np.newaxis] # 0-based numbering # or three = three * np.arange(1, three.shape[0] + 1)[:, np.newaxis] # 1-based numbering ``` fafzal Unladen Swallow Posts: 2 Threads: 1 Joined: Jan 2019 Reputation: 0 Likes received: 0 #3 Jan-10-2019, 06:11 AM (This post was last modified: Jan-10-2019, 06:29 AM by fafzal.) (Jan-10-2019, 05:57 AM)scidam Wrote: No loops needed. Numpy can do all the work by itself: `three = three * np.arange(three.shape[0])[:, np.newaxis] # 0-based numbering # or three = three * np.arange(1, three.shape[0] + 1)[:, np.newaxis] # 1-based numbering ` This works neatly but I'm sorry I should have mentioned that I'm asked to do this using indexing and looping. I was able to find a way and wanted to post back. Here's what worked Thanks for the help ```for i in range (0,5): three[i]=three[i]*(i+1)``` « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post finding p's in words of a multi-line text file johneven 4 100 Yesterday, 07:57 PM Last Post: ichabod801 Finding average in dictionary using for loop Rae 4 156 May-26-2019, 12:06 PM Last Post: DeaD_EyE Find 'greater than' items in list johneven 2 208 Apr-05-2019, 07:22 AM Last Post: perfringo Finding the end point of a path happylol123 1 140 Mar-31-2019, 09:55 AM Last Post: Yoriz Finding Consonants? nlord7 1 172 Feb-28-2019, 03:20 AM Last Post: ichabod801 Help with finding vowels nlord7 1 184 Feb-27-2019, 04:40 AM Last Post: ichabod801 Finding and storing all string with character A at middle position Pippi 2 242 Jan-20-2019, 08:23 AM Last Post: Pippi Python Finding Prime Factors foxman322 1 286 Jan-11-2019, 04:33 PM Last Post: ichabod801 Help adding items to a glossary lga13 2 296 Jan-08-2019, 06:39 AM Last Post: perfringo Finding the Young's Modulues in a curve stress strain AicramM 1 351 Nov-03-2018, 09:01 PM Last Post: Gribouillis

Forum Jump:

Users browsing this thread: 1 Guest(s)