-2

How do I divide every item of a matrix by the first item in its row.

csheth
  • 105
  • 6

3 Answers3

4

For future question please see here and here how to ask a good question and avoid mass downvotes and receive better answers.

In R, there is no need in no loops (and especially in growing objects within them)

Lets say this is your matrix

m <- matrix(1:1344, 84, 16)

All you need to do in order to achieve your desired output

m <- m/m[, 1]

Which will divide each row of your matrix by the first value in each row

in order to save it as a csv format file, see

?write.csv

Community
  • 1
  • 1
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • I'll keep this in mind (not using unnecessary loops) for future. :) – Pj_ Dec 28 '14 at 08:49
  • @Pj_ I'm sorry for being to harsh with you, though I strongly recommend you to read [R-inferno](http://www.burns-stat.com/pages/Tutor/R_inferno.pdf) – David Arenburg Dec 28 '14 at 09:05
  • 1
    That's fine David, this is a good way for me to learn. Your comment was really valuable for me. :-) – Pj_ Dec 28 '14 at 09:07
2

I see that we already have an answer for R, but for those who need pandas:

>>> # Create a dataframe with 84 rows and 16 columns
>>> df = pd.DataFrame(np.random.rand(84, 16))
>>> 
>>> # Divide each row by the first value in the row:
>>> result = df.divide(df[0], axis='rows')
>>> 
>>> # Save to csv
>>> result.to_csv('my_file.csv')

The logic behind the division action: df[0] is the first column of the dataframe. Note that this is because the name of the column is 0, NOT because the index is 0! If the name of the column was "my_col", then the line would be result = df.divide(df["my_col"], axis='rows').

The type of df[0] is pandas.Series.

Now, df.divide can work along the rows (that's why we gave it the parameter axis='rows'), which is what we did: Take a Series with a length that is equal to the number of rows*. Then, each value in the row will be divided by the value in the appropriate location in the Series.

df.divide can also work along the columns, by the way. But then it's easier to do df / df[0].

  • Note: the length doesn't have to be the number of rows, but that's more advanced and out of scope, I think.
shai geva
  • 51
  • 4
0

Because your question is not very clear, consider the following example:

"""populate the matrix"""
import random
matrix = []
matrix_rows = 8
matrix_cols = 6
for i in range(matrix_rows):
    matrix.append([])
    for j in range(matrix_cols):
        matrix[i].append(random.randint(1,500))

for simplicity we'll keep the matrix small, so

>>>matrix
 [[83, 425, 150, 391, 391, 452],
 [447, 32, 36, 344, 374, 315],
 [301, 442, 447, 19, 458, 63],
 [96, 282, 245, 167, 366, 356],
 [300, 19, 481, 180, 385, 17],
 [491, 266, 236, 397, 104, 477],
 [259, 365, 343, 204, 118, 449],
 [20, 192, 461, 160, 83, 391]]

now setup a function to manipulate the matrix:

def divide_matrix(matrix):
    new_matrix = []
    counter_rows = 0
    for row in matrix:
        new_matrix.append([])
        divider = None
        counter_vals = 0
        for value in row:
            if not divider:
                new_matrix[counter_rows].append(value)
                divider = value
            else:
                new_matrix[counter_rows].append(round(value / divider,2))
            counter_vals+=1
        counter_rows+=1
    return new_matrix

and run it:

>>>divide_matrix(matrix)
 [[83, 5.12, 1.81, 4.71, 4.71, 5.45],
 [447, 0.07, 0.08, 0.77, 0.84, 0.7],
 [301, 1.47, 1.49, 0.06, 1.52, 0.21],
 [96, 2.94, 2.55, 1.74, 3.81, 3.71],
 [300, 0.06, 1.6, 0.6, 1.28, 0.06],
 [491, 0.54, 0.48, 0.81, 0.21, 0.97],
 [259, 1.41, 1.32, 0.79, 0.46, 1.73],
 [20, 9.6, 23.05, 8.0, 4.15, 19.55]]
Stefan
  • 2,164
  • 1
  • 23
  • 40