Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix
#1
Hi,
How to check if all elements of a matrix have the same value? How to do it without numpy and other similar extras?
Thank you!
Reply
#2
The all function is your friend. You could do all on each row as a list comprehension, and then all on the result. Depending on the size of the matrix, doing it as a loop so you can short circuit in may be good. Then you would loop through the rows, any on each row, break if it's false, and else on the loop for if all of them are true.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you for your prompt reply. But unfortunately I am such a total beginnier that your advice it too complicated for me. I know about "loop" and "all" but I am unable to get it working. I have the following:
def result(matrix):
    for row in matrix:
        for element in row:
            if all(matrix) == "X":
                return True
            else:
                return False
But it returns a wrong result.
I would be very grateful if you could advise me on this.
Thank you!
Reply
#4
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

Let's forget about all. You have the right idea, but you don't want to return True until the end of the loop. You are returning True when you get one correct result, you only want to do that when all the results are True. However, as soon as one of the checks fails (if element != 'X':) you can return False.

So switch the conditional in the loop to return False if the element isn't correct. Then after both loops are done, return True.

Note that your code checks that all the items are 'X'. To check that they are all equal (no matter what they are), check against matrix[0][0] instead (that's the first item in the first row).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Feb-02-2019, 04:30 AM)ichabod801 Wrote: Note that your code checks that all the items are 'X'. To check that they are all equal (no matter what they are), check against matrix[0][0] instead (that's the first item in the first row).

I don't know is it important or not but just observing that in case of empty matrix (list of empty lists) checking against matrix[0][0] will give IndexError.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
There are also some other approaches to explore:

- there are sets in Python (from documentation: "Basic uses include membership testing and eliminating duplicate entries."). Problem with solution below is that it will not short circuit (following code assumes that in case of empty matrix elements are considered equal as well, if its not the case comparison should be == 1):

In [1]: m = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

In [2]: len({el for row in m for el in row}) <= 1    # 0 when empty matrix, 1 when all equal
Out[2]: True

In [3]: n = [[1, 1, 1], [1, 1, 1], [1, 2, 1]]

In [4]: len({el for row in n for el in row}) <= 1
Out[4]: False
- Python has built-in itertools module and it's documentation contains recipies where one of them is:

def all_equal(iterable):
    "Returns True if all the elements are equal to each other"
    g = groupby(iterable)
    return next(g, True) and not next(g, False)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
Thank you very much for your help with this! :)
Reply
#8
(Feb-02-2019, 04:58 AM)perfringo Wrote: I don't know is it important or not but just observing that in case of empty matrix (list of empty lists) checking against matrix[0][0] will give IndexError.

I think it should. Checking that all the items in an empty matrix are equal doesn't make sense, so I think returning an IndexError is a good idea. Sort of like popping from an empty list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,300 May-03-2021, 06:30 AM
Last Post: Gribouillis
  matrix from matrix python numpy array shei7141 1 3,642 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020