Get diagonal from matrix











up vote
0
down vote

favorite












I want to get the left and right diagonals of a matrix containing the element with the given column and row indexes.



For example:



rightDiagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [2,4]

leftDiagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [4,8]


Thank you.



This is my try:



left_diagonal_left(Board, 0, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List).

left_diagonal_left(Board, Row_Index, 0, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List).

left_diagonal_left(Board, Row_Index, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List),
R = Row_Index - 1,
C = Column_Index - 1,
left_diagonal_left(Board, R, C, New_List, Newer_List).

left_diagonal_right(Board, 6, Column_Index, List, New_List).
left_diagonal_right(Board, Row_Index, 6, List, New_List).

left_diagonal_right(Board, Row_Index, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List),
R = Row_Index + 1,
C = Column_Index + 1,
left_diagonal_right(Board, R, C, New_List, Newer_List).

left_diagonal(Board, Row_Index, Column_Index, List, Newer_List) :-
left_diagonal_left(Board, Row_Index, Column_Index, List, New_List),
R = Row_Index + 1,
C = Column_Index + 1,
left_diagonal_right(Board, R, C, New_List, Newer_List).


The problem is that the append() fails.










share|improve this question




















  • 1




    What is not working with your attempt?
    – Willem Van Onsem
    Nov 11 at 14:15










  • @WillemVanOnsem Sorry, I've edited the post with my try :)
    – JotaB
    Nov 11 at 16:46










  • So given I understood it correctly, you want to "full" diagonal, not start at a specific row, right?
    – Willem Van Onsem
    Nov 11 at 17:02










  • I think that append fails because yu append a value to a list , you should append a list to a list, so you should try append(List, [Value], New_List),.
    – joel76
    Nov 11 at 17:05















up vote
0
down vote

favorite












I want to get the left and right diagonals of a matrix containing the element with the given column and row indexes.



For example:



rightDiagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [2,4]

leftDiagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [4,8]


Thank you.



This is my try:



left_diagonal_left(Board, 0, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List).

left_diagonal_left(Board, Row_Index, 0, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List).

left_diagonal_left(Board, Row_Index, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List),
R = Row_Index - 1,
C = Column_Index - 1,
left_diagonal_left(Board, R, C, New_List, Newer_List).

left_diagonal_right(Board, 6, Column_Index, List, New_List).
left_diagonal_right(Board, Row_Index, 6, List, New_List).

left_diagonal_right(Board, Row_Index, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List),
R = Row_Index + 1,
C = Column_Index + 1,
left_diagonal_right(Board, R, C, New_List, Newer_List).

left_diagonal(Board, Row_Index, Column_Index, List, Newer_List) :-
left_diagonal_left(Board, Row_Index, Column_Index, List, New_List),
R = Row_Index + 1,
C = Column_Index + 1,
left_diagonal_right(Board, R, C, New_List, Newer_List).


The problem is that the append() fails.










share|improve this question




















  • 1




    What is not working with your attempt?
    – Willem Van Onsem
    Nov 11 at 14:15










  • @WillemVanOnsem Sorry, I've edited the post with my try :)
    – JotaB
    Nov 11 at 16:46










  • So given I understood it correctly, you want to "full" diagonal, not start at a specific row, right?
    – Willem Van Onsem
    Nov 11 at 17:02










  • I think that append fails because yu append a value to a list , you should append a list to a list, so you should try append(List, [Value], New_List),.
    – joel76
    Nov 11 at 17:05













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to get the left and right diagonals of a matrix containing the element with the given column and row indexes.



For example:



rightDiagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [2,4]

leftDiagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [4,8]


Thank you.



This is my try:



left_diagonal_left(Board, 0, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List).

left_diagonal_left(Board, Row_Index, 0, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List).

left_diagonal_left(Board, Row_Index, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List),
R = Row_Index - 1,
C = Column_Index - 1,
left_diagonal_left(Board, R, C, New_List, Newer_List).

left_diagonal_right(Board, 6, Column_Index, List, New_List).
left_diagonal_right(Board, Row_Index, 6, List, New_List).

left_diagonal_right(Board, Row_Index, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List),
R = Row_Index + 1,
C = Column_Index + 1,
left_diagonal_right(Board, R, C, New_List, Newer_List).

left_diagonal(Board, Row_Index, Column_Index, List, Newer_List) :-
left_diagonal_left(Board, Row_Index, Column_Index, List, New_List),
R = Row_Index + 1,
C = Column_Index + 1,
left_diagonal_right(Board, R, C, New_List, Newer_List).


The problem is that the append() fails.










share|improve this question















I want to get the left and right diagonals of a matrix containing the element with the given column and row indexes.



For example:



rightDiagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [2,4]

leftDiagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [4,8]


Thank you.



This is my try:



left_diagonal_left(Board, 0, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List).

left_diagonal_left(Board, Row_Index, 0, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List).

left_diagonal_left(Board, Row_Index, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List),
R = Row_Index - 1,
C = Column_Index - 1,
left_diagonal_left(Board, R, C, New_List, Newer_List).

left_diagonal_right(Board, 6, Column_Index, List, New_List).
left_diagonal_right(Board, Row_Index, 6, List, New_List).

left_diagonal_right(Board, Row_Index, Column_Index, List, New_List) :-
get_value_from_matrix(Board, Row_Index, Column_Index, Value),
append(List, Value, New_List),
R = Row_Index + 1,
C = Column_Index + 1,
left_diagonal_right(Board, R, C, New_List, Newer_List).

left_diagonal(Board, Row_Index, Column_Index, List, Newer_List) :-
left_diagonal_left(Board, Row_Index, Column_Index, List, New_List),
R = Row_Index + 1,
C = Column_Index + 1,
left_diagonal_right(Board, R, C, New_List, Newer_List).


The problem is that the append() fails.







prolog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:46

























asked Nov 11 at 14:13









JotaB

446




446








  • 1




    What is not working with your attempt?
    – Willem Van Onsem
    Nov 11 at 14:15










  • @WillemVanOnsem Sorry, I've edited the post with my try :)
    – JotaB
    Nov 11 at 16:46










  • So given I understood it correctly, you want to "full" diagonal, not start at a specific row, right?
    – Willem Van Onsem
    Nov 11 at 17:02










  • I think that append fails because yu append a value to a list , you should append a list to a list, so you should try append(List, [Value], New_List),.
    – joel76
    Nov 11 at 17:05














  • 1




    What is not working with your attempt?
    – Willem Van Onsem
    Nov 11 at 14:15










  • @WillemVanOnsem Sorry, I've edited the post with my try :)
    – JotaB
    Nov 11 at 16:46










  • So given I understood it correctly, you want to "full" diagonal, not start at a specific row, right?
    – Willem Van Onsem
    Nov 11 at 17:02










  • I think that append fails because yu append a value to a list , you should append a list to a list, so you should try append(List, [Value], New_List),.
    – joel76
    Nov 11 at 17:05








1




1




What is not working with your attempt?
– Willem Van Onsem
Nov 11 at 14:15




What is not working with your attempt?
– Willem Van Onsem
Nov 11 at 14:15












@WillemVanOnsem Sorry, I've edited the post with my try :)
– JotaB
Nov 11 at 16:46




@WillemVanOnsem Sorry, I've edited the post with my try :)
– JotaB
Nov 11 at 16:46












So given I understood it correctly, you want to "full" diagonal, not start at a specific row, right?
– Willem Van Onsem
Nov 11 at 17:02




So given I understood it correctly, you want to "full" diagonal, not start at a specific row, right?
– Willem Van Onsem
Nov 11 at 17:02












I think that append fails because yu append a value to a list , you should append a list to a list, so you should try append(List, [Value], New_List),.
– joel76
Nov 11 at 17:05




I think that append fails because yu append a value to a list , you should append a list to a list, so you should try append(List, [Value], New_List),.
– joel76
Nov 11 at 17:05












1 Answer
1






active

oldest

votes

















up vote
0
down vote













We can make a generic predicate that can walk in both directions (to the left, and to the right).



We basically here need two parameters: the index of the first column, and and the "direction" in which we walk, this is -1 for "right", and 1 for "left".



We can thus implement a predicate diagonal/4 that will generate such diagional.



Basically there are three cases we need to take into account:




  1. the matrix has "no rows", in that case the diagonal is an empty list;

  2. the matrix has at least one row, but the index is too low/high, in that case the diagonal is an empty list as well; and

  3. the matrix has at least one row, and the index is not out of range, in that case we add the element of the first row for the given column, and recursively call the predicate where we shift the column one to the right, or one to the left.


So we can implement this with:



diagonal(, _, _, ).
diagonal([Row|Rest], Col, DCol, Result) :-
( nth0(Col, Row, El)
-> (Result = [El | R2],
Col2 is Col + DCol,
diagonal(Rest, Col2, DCol, R2))
; Result = ).


So now we can obtain diagonals with:



?- diagonal([[4,5,6],[7,8,9]], 1, -1, Diagonal).
Diagonal = [5, 7].

?- diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, -1, Diagonal).
Diagonal = [2, 4].

?- diagonal([[4,5,6],[7,8,9]], 0, 1, Diagonal).
Diagonal = [4, 8].


Of course the above is not yet complete: since in the question, one enters two coordinates. In case we take the right coordinate however, for a coordinate pair (X, Y), this is equivalent to (X+Y, 0). The same for left diagonals: the diagonal containing (X, Y) is the same as the one containing (0, Y-X). Note however that it is possible that X+Y, or Y-X are out of range, so we can first "normalize" the coordinate, but in case the coordinate is "too high", or "too low", we thus will need a to drop a certain number of rows, and each time updating the number until we reach a value that is in range.



So we can implement a predicate that first makes some iterations until it finds a valid index, and then passes control to the diagonal/4 predicate, for example prediagonal/4:



prediagonal(, _, _, ).
prediagonal([Row|Rest], Col, DCol, Result) :-
Col2 is Col + DCol,
( nth0(Col, Row, El)
-> (Result = [El | R2],
diagonal(Rest, Col2, DCol, R2))
; prediagonal(Rest, Col2, DCol, Result)).


So now we can write our left_diagonal/4 and right_diagonal in terms of prediagonal/4:



left_diagonal(M, R, C, Diagonal) :-
Delta is C-R,
prediagonal(M, Delta, 1, Diagonal).

right_diagonal(M, R, C, Diagonal) :-
Delta is R+C,
prediagonal(M, Delta, -1, Diagonal).


this then gives us:



?- right_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [2, 4].

?- left_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
Diagonal = [4, 8].


The above is not the most elegant solution. It might be better to "merge" diagonal/4 and prediagonal/4 in one predicate. I leave this as an exercise.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249583%2fget-diagonal-from-matrix%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    We can make a generic predicate that can walk in both directions (to the left, and to the right).



    We basically here need two parameters: the index of the first column, and and the "direction" in which we walk, this is -1 for "right", and 1 for "left".



    We can thus implement a predicate diagonal/4 that will generate such diagional.



    Basically there are three cases we need to take into account:




    1. the matrix has "no rows", in that case the diagonal is an empty list;

    2. the matrix has at least one row, but the index is too low/high, in that case the diagonal is an empty list as well; and

    3. the matrix has at least one row, and the index is not out of range, in that case we add the element of the first row for the given column, and recursively call the predicate where we shift the column one to the right, or one to the left.


    So we can implement this with:



    diagonal(, _, _, ).
    diagonal([Row|Rest], Col, DCol, Result) :-
    ( nth0(Col, Row, El)
    -> (Result = [El | R2],
    Col2 is Col + DCol,
    diagonal(Rest, Col2, DCol, R2))
    ; Result = ).


    So now we can obtain diagonals with:



    ?- diagonal([[4,5,6],[7,8,9]], 1, -1, Diagonal).
    Diagonal = [5, 7].

    ?- diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, -1, Diagonal).
    Diagonal = [2, 4].

    ?- diagonal([[4,5,6],[7,8,9]], 0, 1, Diagonal).
    Diagonal = [4, 8].


    Of course the above is not yet complete: since in the question, one enters two coordinates. In case we take the right coordinate however, for a coordinate pair (X, Y), this is equivalent to (X+Y, 0). The same for left diagonals: the diagonal containing (X, Y) is the same as the one containing (0, Y-X). Note however that it is possible that X+Y, or Y-X are out of range, so we can first "normalize" the coordinate, but in case the coordinate is "too high", or "too low", we thus will need a to drop a certain number of rows, and each time updating the number until we reach a value that is in range.



    So we can implement a predicate that first makes some iterations until it finds a valid index, and then passes control to the diagonal/4 predicate, for example prediagonal/4:



    prediagonal(, _, _, ).
    prediagonal([Row|Rest], Col, DCol, Result) :-
    Col2 is Col + DCol,
    ( nth0(Col, Row, El)
    -> (Result = [El | R2],
    diagonal(Rest, Col2, DCol, R2))
    ; prediagonal(Rest, Col2, DCol, Result)).


    So now we can write our left_diagonal/4 and right_diagonal in terms of prediagonal/4:



    left_diagonal(M, R, C, Diagonal) :-
    Delta is C-R,
    prediagonal(M, Delta, 1, Diagonal).

    right_diagonal(M, R, C, Diagonal) :-
    Delta is R+C,
    prediagonal(M, Delta, -1, Diagonal).


    this then gives us:



    ?- right_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
    Diagonal = [2, 4].

    ?- left_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
    Diagonal = [4, 8].


    The above is not the most elegant solution. It might be better to "merge" diagonal/4 and prediagonal/4 in one predicate. I leave this as an exercise.






    share|improve this answer

























      up vote
      0
      down vote













      We can make a generic predicate that can walk in both directions (to the left, and to the right).



      We basically here need two parameters: the index of the first column, and and the "direction" in which we walk, this is -1 for "right", and 1 for "left".



      We can thus implement a predicate diagonal/4 that will generate such diagional.



      Basically there are three cases we need to take into account:




      1. the matrix has "no rows", in that case the diagonal is an empty list;

      2. the matrix has at least one row, but the index is too low/high, in that case the diagonal is an empty list as well; and

      3. the matrix has at least one row, and the index is not out of range, in that case we add the element of the first row for the given column, and recursively call the predicate where we shift the column one to the right, or one to the left.


      So we can implement this with:



      diagonal(, _, _, ).
      diagonal([Row|Rest], Col, DCol, Result) :-
      ( nth0(Col, Row, El)
      -> (Result = [El | R2],
      Col2 is Col + DCol,
      diagonal(Rest, Col2, DCol, R2))
      ; Result = ).


      So now we can obtain diagonals with:



      ?- diagonal([[4,5,6],[7,8,9]], 1, -1, Diagonal).
      Diagonal = [5, 7].

      ?- diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, -1, Diagonal).
      Diagonal = [2, 4].

      ?- diagonal([[4,5,6],[7,8,9]], 0, 1, Diagonal).
      Diagonal = [4, 8].


      Of course the above is not yet complete: since in the question, one enters two coordinates. In case we take the right coordinate however, for a coordinate pair (X, Y), this is equivalent to (X+Y, 0). The same for left diagonals: the diagonal containing (X, Y) is the same as the one containing (0, Y-X). Note however that it is possible that X+Y, or Y-X are out of range, so we can first "normalize" the coordinate, but in case the coordinate is "too high", or "too low", we thus will need a to drop a certain number of rows, and each time updating the number until we reach a value that is in range.



      So we can implement a predicate that first makes some iterations until it finds a valid index, and then passes control to the diagonal/4 predicate, for example prediagonal/4:



      prediagonal(, _, _, ).
      prediagonal([Row|Rest], Col, DCol, Result) :-
      Col2 is Col + DCol,
      ( nth0(Col, Row, El)
      -> (Result = [El | R2],
      diagonal(Rest, Col2, DCol, R2))
      ; prediagonal(Rest, Col2, DCol, Result)).


      So now we can write our left_diagonal/4 and right_diagonal in terms of prediagonal/4:



      left_diagonal(M, R, C, Diagonal) :-
      Delta is C-R,
      prediagonal(M, Delta, 1, Diagonal).

      right_diagonal(M, R, C, Diagonal) :-
      Delta is R+C,
      prediagonal(M, Delta, -1, Diagonal).


      this then gives us:



      ?- right_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
      Diagonal = [2, 4].

      ?- left_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
      Diagonal = [4, 8].


      The above is not the most elegant solution. It might be better to "merge" diagonal/4 and prediagonal/4 in one predicate. I leave this as an exercise.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        We can make a generic predicate that can walk in both directions (to the left, and to the right).



        We basically here need two parameters: the index of the first column, and and the "direction" in which we walk, this is -1 for "right", and 1 for "left".



        We can thus implement a predicate diagonal/4 that will generate such diagional.



        Basically there are three cases we need to take into account:




        1. the matrix has "no rows", in that case the diagonal is an empty list;

        2. the matrix has at least one row, but the index is too low/high, in that case the diagonal is an empty list as well; and

        3. the matrix has at least one row, and the index is not out of range, in that case we add the element of the first row for the given column, and recursively call the predicate where we shift the column one to the right, or one to the left.


        So we can implement this with:



        diagonal(, _, _, ).
        diagonal([Row|Rest], Col, DCol, Result) :-
        ( nth0(Col, Row, El)
        -> (Result = [El | R2],
        Col2 is Col + DCol,
        diagonal(Rest, Col2, DCol, R2))
        ; Result = ).


        So now we can obtain diagonals with:



        ?- diagonal([[4,5,6],[7,8,9]], 1, -1, Diagonal).
        Diagonal = [5, 7].

        ?- diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, -1, Diagonal).
        Diagonal = [2, 4].

        ?- diagonal([[4,5,6],[7,8,9]], 0, 1, Diagonal).
        Diagonal = [4, 8].


        Of course the above is not yet complete: since in the question, one enters two coordinates. In case we take the right coordinate however, for a coordinate pair (X, Y), this is equivalent to (X+Y, 0). The same for left diagonals: the diagonal containing (X, Y) is the same as the one containing (0, Y-X). Note however that it is possible that X+Y, or Y-X are out of range, so we can first "normalize" the coordinate, but in case the coordinate is "too high", or "too low", we thus will need a to drop a certain number of rows, and each time updating the number until we reach a value that is in range.



        So we can implement a predicate that first makes some iterations until it finds a valid index, and then passes control to the diagonal/4 predicate, for example prediagonal/4:



        prediagonal(, _, _, ).
        prediagonal([Row|Rest], Col, DCol, Result) :-
        Col2 is Col + DCol,
        ( nth0(Col, Row, El)
        -> (Result = [El | R2],
        diagonal(Rest, Col2, DCol, R2))
        ; prediagonal(Rest, Col2, DCol, Result)).


        So now we can write our left_diagonal/4 and right_diagonal in terms of prediagonal/4:



        left_diagonal(M, R, C, Diagonal) :-
        Delta is C-R,
        prediagonal(M, Delta, 1, Diagonal).

        right_diagonal(M, R, C, Diagonal) :-
        Delta is R+C,
        prediagonal(M, Delta, -1, Diagonal).


        this then gives us:



        ?- right_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
        Diagonal = [2, 4].

        ?- left_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
        Diagonal = [4, 8].


        The above is not the most elegant solution. It might be better to "merge" diagonal/4 and prediagonal/4 in one predicate. I leave this as an exercise.






        share|improve this answer












        We can make a generic predicate that can walk in both directions (to the left, and to the right).



        We basically here need two parameters: the index of the first column, and and the "direction" in which we walk, this is -1 for "right", and 1 for "left".



        We can thus implement a predicate diagonal/4 that will generate such diagional.



        Basically there are three cases we need to take into account:




        1. the matrix has "no rows", in that case the diagonal is an empty list;

        2. the matrix has at least one row, but the index is too low/high, in that case the diagonal is an empty list as well; and

        3. the matrix has at least one row, and the index is not out of range, in that case we add the element of the first row for the given column, and recursively call the predicate where we shift the column one to the right, or one to the left.


        So we can implement this with:



        diagonal(, _, _, ).
        diagonal([Row|Rest], Col, DCol, Result) :-
        ( nth0(Col, Row, El)
        -> (Result = [El | R2],
        Col2 is Col + DCol,
        diagonal(Rest, Col2, DCol, R2))
        ; Result = ).


        So now we can obtain diagonals with:



        ?- diagonal([[4,5,6],[7,8,9]], 1, -1, Diagonal).
        Diagonal = [5, 7].

        ?- diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, -1, Diagonal).
        Diagonal = [2, 4].

        ?- diagonal([[4,5,6],[7,8,9]], 0, 1, Diagonal).
        Diagonal = [4, 8].


        Of course the above is not yet complete: since in the question, one enters two coordinates. In case we take the right coordinate however, for a coordinate pair (X, Y), this is equivalent to (X+Y, 0). The same for left diagonals: the diagonal containing (X, Y) is the same as the one containing (0, Y-X). Note however that it is possible that X+Y, or Y-X are out of range, so we can first "normalize" the coordinate, but in case the coordinate is "too high", or "too low", we thus will need a to drop a certain number of rows, and each time updating the number until we reach a value that is in range.



        So we can implement a predicate that first makes some iterations until it finds a valid index, and then passes control to the diagonal/4 predicate, for example prediagonal/4:



        prediagonal(, _, _, ).
        prediagonal([Row|Rest], Col, DCol, Result) :-
        Col2 is Col + DCol,
        ( nth0(Col, Row, El)
        -> (Result = [El | R2],
        diagonal(Rest, Col2, DCol, R2))
        ; prediagonal(Rest, Col2, DCol, Result)).


        So now we can write our left_diagonal/4 and right_diagonal in terms of prediagonal/4:



        left_diagonal(M, R, C, Diagonal) :-
        Delta is C-R,
        prediagonal(M, Delta, 1, Diagonal).

        right_diagonal(M, R, C, Diagonal) :-
        Delta is R+C,
        prediagonal(M, Delta, -1, Diagonal).


        this then gives us:



        ?- right_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
        Diagonal = [2, 4].

        ?- left_diagonal([[1,2,3],[4,5,6],[7,8,9]], 1, 0, Diagonal).
        Diagonal = [4, 8].


        The above is not the most elegant solution. It might be better to "merge" diagonal/4 and prediagonal/4 in one predicate. I leave this as an exercise.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 20:00









        Willem Van Onsem

        141k16132225




        141k16132225






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249583%2fget-diagonal-from-matrix%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)