3
votes

I want to sum up each row and column in matrix and check whether all of it are the same. I want to use sum of row and transpose method to do it, but I had no ideas how it can check whether sum of every row and column are the same.

Can please give me some algorithm guidance?

2

2 Answers

1
votes

Just always use the same variable when computing the sum.
This example shows how it's done for rows.

checkRowSums([], _).
checkRowSums([Row|R], Sum) :- rowSum(Row, Sum), checkRowSums(R, Sum).

?- checkRowSums([[1, 5], [2, 4], [1, 2, 3]], _).
0
votes

To check the rows in a column you can use the predicate sum_list:

sum_list([], 0). sum_list([H|T], Sum) :- sum_list(T, Rest), Sum is H + Rest.

Then use sum_list to find the total sum of the rows, like this:

sum_rows([],[]). sum_rows([Head|Tail], [Ret|Return]) :- sum_list(Head, Ret), sum_rows(Tail, Return).

This will return a list of the row sums.