Rapidistul
Rapidistul

Reputation: 564

SWI-Prolog Matrix. How to find the value of an element by indexes?

map(1,
   [[1,_,_,_,_,_,_,_,_,_],
    [1,1,_,_,1,1,_,_,_,_],
    [_,1,_,_,1,_,_,_,_,_],
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,1,_,_,_,1,_,_,_],
    [_,1,1,_,_,_,_,_,_,_],
    [_,_,_,_,_,_,_,1,1,_],
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,_,_,_,_,_,_,_,_]]).

This is my matrix. How can I find the value of the element from second row and first column?. I have no idea how i can do it. Who can help me?

Thank you very much!

Upvotes: 2

Views: 1085

Answers (1)

CapelliC
CapelliC

Reputation: 60034

you can write a 4 argument predicate, at/4

at(Mat, Row, Col, Val) :- nth1(Row, Mat, ARow), nth1(Col, ARow, Val).

and call it like

test :- map(_, Map), at(Map, 2, 1, Val), write(Val).

Upvotes: 2

Related Questions