1

I am very new to Python and struggling to find a way to enter a matrix of data. What I need to do is:

  1. Ask user how many rows
  2. Ask user how many columns 3 Enable user to type in 1 or 0 values for the specified row x column matrix

I'm actually trying to replicate what was previously undertaken in an old PCW BASIC program! In that version, the user was asked to type a 1 or 0 individually, i.e for a 3 x 3 matrix they would be asked to enter Row 1, Col 1? [Enter] Row 1, Col 2 [Enter] Row 1, Col 3 [Enter] Row 2, Col 1 [Enter] .... etc.

Ideally it would be better if the user was able to type one complete row at a time, i.e 1 0 1 [Enter] 1 1 0 [Enter] 0 1 0 [Enter] or even better in a spreadsheet-like grid but I suspect that is too ambitious for someone just starting out in Python. For each matrix the user would first have entered row and column title labels so it would be preferable to show these in the screen output once the data has been entered.

Beyond this stage there is a series of fairly basic mathematical operations to be completed on the data.

From my initial reading I initially got the impression that I might need to use NumPy or pandas? I have read a bit about these but can't see anything which fairly closely resembles what I am trying to achieve. Now I'm beginning to think that Lists may suffice?

If anyone can help point me in the right direction or give me a few pointers that would be much appreciated.

To illustrate what I'm trying to replicate, this is an extract from the original BASIC code:

1100 INPUT "HOW MANY ELEMENTS (COLUMNS) HAVE YOU"J
1110 PRINT
1120 INPUT "HOW MANY VARIABLES (ROWS) HAVE YOU"I
1130 PRINT
1140 PRINT "INPUT DATA,ROW BY ROW,AS FOLLOWS:"
1150 PRINT "TYPE '0' FOR A VOID"
1160 PRINT "TYPE '1' FOR AN INCIDENT"
1170 PRINT
1180 FOR R = 1 TO I
1190 FOR C = 1 TO J
1200 PRINT "ROW "R;": COLUMN "C;
1210 INPUT "INCIDENT OR VOID"R%(R,C)
1220 NEXT C
1230 NEXT R

Thanks very much!

Robert

2 Answers 2

1

You could ask the user to enter the rows one by one and finish with an empty row (without asking for the number of rows before):

print ('Enter your row one by one (pressing enter between each row). To terminate, enter an empty row.')
M = []
while True:
    r = input ('Next row > ')
    if not r.strip (): # Empty input
        break
    M.append(list(map(int, r.split())))
print('M =', M)

Result:

Enter your [...] empty row.
Next row > 0 1 0
Next row > 1 1 0
Next row > 0 0 1
Next row > 
M = [[0, 1, 0], [1, 1, 0], [0, 0, 1]]

numpy is a scientific library which aims at enabling « MATLAB like » code in python (faster manipulation of matrix, and much more). Depending on what you want to do with your matrix, using numpy may not be the fastest way.

A little example (using the matrix M defined before, and regarding your comment):

>>> import numpy
>>> a = numpy.array (M)
>>> a
array([[0, 1, 0],
       [1, 1, 0],
       [0, 0, 1]])
>>> a.sum (0) # Sum over the first axis (row)
array([1, 2, 1])
>>> a.sum (0) % 2 # Sum + Modulo over the result
array([1, 0, 1], dtype=int32)
>>> b = 1 - a.sum (0) % 2 # Sum + Modulo + Inverse 1 and 0
>>> b
array([0, 1, 0], dtype=int32)
>>> a == b # check for each row
array([[ True,  True,  True],
       [False,  True,  True],
       [ True, False, False]], dtype=bool)
>>> (a == b).all (1) # Check for each raw if all column are True
array([ True, False, False], dtype=bool)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. The aim of the program is to undertake a simplified form of factor analysis. It needs to total the individual rows, convert the new summary rows back to 1's and 0's and in some cases transpose the values such that 1 becomes 0 and vice versa if that gets a better fit. It then compared the new summary row with each of the original rows and records the number of column matches in each row in some new columns. I'm not looking for help on these steps, for now anyway, but just giving an idea of the kind of data manipulation I need to achieve. Is this something numpy would be useful for?
@user5084929 Yes, numpy would be useful in your case. With numpy you can do global operation on your matrix like summing over a column, adding (or removing) a specifing value to each cell, etc. I will add a little example at the end of my answer.
0
for i in range(0,columns):
    for j in range(0,rows):
        A[i][j] = int(input("Enter the value at row %d , column %d : "%(j,i)))

This is the basic prototype for what you want, change the names and functionality as needed.
To print :

for i in range(0,columns):
    for j in range(0,rows):
        print("The value at row %d , column %d is : %d : "%(j,i,A[i,j]))

Comments

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.