Bottom Page

Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding Row Number for Items in 2D array
#1
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])
Quote
#2
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
Quote
#3
(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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Finding and storing all string with character A at middle position Pippi 2 65 Jan-20-2019, 08:23 AM
Last Post: Pippi
  Python Finding Prime Factors foxman322 1 117 Jan-11-2019, 04:33 PM
Last Post: ichabod801
  Help adding items to a glossary lga13 2 117 Jan-08-2019, 06:39 AM
Last Post: perfringo
  Finding the Young's Modulues in a curve stress strain AicramM 1 171 Nov-03-2018, 09:01 PM
Last Post: Gribouillis
  How to add items within a list Mrocks22 2 186 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  finding the minimum value out of a set of inputs kannan 1 198 Oct-30-2018, 01:52 PM
Last Post: ichabod801
  finding almost palindromes leahma 1 208 Oct-20-2018, 04:14 PM
Last Post: micseydel
  Divide a number - Multiple levels - Sum of all numbers equal to input number pythoneer 17 1,782 Apr-20-2018, 04:07 AM
Last Post: pythoneer
  Finding the average of numbers in a txt file piday 1 1,956 Feb-27-2018, 04:00 AM
Last Post: Larz60+
  Help with array smallest biggest number thanikos 4 692 Nov-30-2017, 01:06 PM
Last Post: thanikos

Forum Jump:


Users browsing this thread: 1 Guest(s)