Trim that distracting background off!

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this problem! But - as usual - I am quite lazy, so I need someone to do this for me...



Task & Rules



Given a binary matrix representing the image, output the dimensions (width and height) of the smallest sub-matrix that contains all the $1$s in the original matrix. A sub-matrix is a block of adjacent entries from the original matrix. Equivalently, it is a new matrix formed by overlapping a subset of adjacent rows and a subset of adjacent columns of the original.



  • It is allowed to take the width and the height of the matrix as input as well.

  • The input is guaranteed to contain at least one $1$.

  • You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so try to complete the task in the least bytes you can manage in your language of choice.

Example



$$left[beginmatrix
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0\
colorred0&colorblue1&colorblue0&colorblue1&colorblue0&colorblue0\
colorred0&colorblue1&colorblue1&colorblue0&colorblue1&colorblue1\
colorred0&colorblue0&colorblue1&colorblue0&colorblue1&colorblue0\
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0endmatrixright] longrightarrow left[beginmatrix1&0&1&0&0\1&1&0&1&1\0&1&0&1&0endmatrixright]longrightarrow(5,3)$$



Test cases




Input | Output

[[0,1,0,0,0,1,0]]
--> (5,1) or (1,5)

[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0]]
--> (3,2) or (2,3)

[[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
--> (4,4)

[[0,0,0,0,0,0],[0,1,0,1,0,1],[0,0,0,0,0,0]]
--> (5,1) or (1,5)

[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[0,0,0,0,0]]
--> (3,3)

[[0,0,0,0,0,0],[0,1,0,1,0,0],[0,1,1,0,1,1],[0,0,1,0,1,0],[0,0,0,0,0,0]]
--> (5,3) or (3,5)









share|improve this question



















  • 1




    This feels very familiar; was it in the Sandbox for a while?
    – Shaggy
    29 mins ago






  • 2




    Possible duplicate of Remove surrounding zeroes of a 2d array.
    – Neil
    27 mins ago










  • A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
    – Shaggy
    13 mins ago














up vote
3
down vote

favorite












Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this problem! But - as usual - I am quite lazy, so I need someone to do this for me...



Task & Rules



Given a binary matrix representing the image, output the dimensions (width and height) of the smallest sub-matrix that contains all the $1$s in the original matrix. A sub-matrix is a block of adjacent entries from the original matrix. Equivalently, it is a new matrix formed by overlapping a subset of adjacent rows and a subset of adjacent columns of the original.



  • It is allowed to take the width and the height of the matrix as input as well.

  • The input is guaranteed to contain at least one $1$.

  • You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so try to complete the task in the least bytes you can manage in your language of choice.

Example



$$left[beginmatrix
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0\
colorred0&colorblue1&colorblue0&colorblue1&colorblue0&colorblue0\
colorred0&colorblue1&colorblue1&colorblue0&colorblue1&colorblue1\
colorred0&colorblue0&colorblue1&colorblue0&colorblue1&colorblue0\
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0endmatrixright] longrightarrow left[beginmatrix1&0&1&0&0\1&1&0&1&1\0&1&0&1&0endmatrixright]longrightarrow(5,3)$$



Test cases




Input | Output

[[0,1,0,0,0,1,0]]
--> (5,1) or (1,5)

[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0]]
--> (3,2) or (2,3)

[[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
--> (4,4)

[[0,0,0,0,0,0],[0,1,0,1,0,1],[0,0,0,0,0,0]]
--> (5,1) or (1,5)

[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[0,0,0,0,0]]
--> (3,3)

[[0,0,0,0,0,0],[0,1,0,1,0,0],[0,1,1,0,1,1],[0,0,1,0,1,0],[0,0,0,0,0,0]]
--> (5,3) or (3,5)









share|improve this question



















  • 1




    This feels very familiar; was it in the Sandbox for a while?
    – Shaggy
    29 mins ago






  • 2




    Possible duplicate of Remove surrounding zeroes of a 2d array.
    – Neil
    27 mins ago










  • A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
    – Shaggy
    13 mins ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this problem! But - as usual - I am quite lazy, so I need someone to do this for me...



Task & Rules



Given a binary matrix representing the image, output the dimensions (width and height) of the smallest sub-matrix that contains all the $1$s in the original matrix. A sub-matrix is a block of adjacent entries from the original matrix. Equivalently, it is a new matrix formed by overlapping a subset of adjacent rows and a subset of adjacent columns of the original.



  • It is allowed to take the width and the height of the matrix as input as well.

  • The input is guaranteed to contain at least one $1$.

  • You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so try to complete the task in the least bytes you can manage in your language of choice.

Example



$$left[beginmatrix
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0\
colorred0&colorblue1&colorblue0&colorblue1&colorblue0&colorblue0\
colorred0&colorblue1&colorblue1&colorblue0&colorblue1&colorblue1\
colorred0&colorblue0&colorblue1&colorblue0&colorblue1&colorblue0\
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0endmatrixright] longrightarrow left[beginmatrix1&0&1&0&0\1&1&0&1&1\0&1&0&1&0endmatrixright]longrightarrow(5,3)$$



Test cases




Input | Output

[[0,1,0,0,0,1,0]]
--> (5,1) or (1,5)

[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0]]
--> (3,2) or (2,3)

[[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
--> (4,4)

[[0,0,0,0,0,0],[0,1,0,1,0,1],[0,0,0,0,0,0]]
--> (5,1) or (1,5)

[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[0,0,0,0,0]]
--> (3,3)

[[0,0,0,0,0,0],[0,1,0,1,0,0],[0,1,1,0,1,1],[0,0,1,0,1,0],[0,0,0,0,0,0]]
--> (5,3) or (3,5)









share|improve this question















Isn't it annoying when you're taking a picture, but the background detracts from the actual substance of the image? I'd say it is. I need to know how much I should crop so that I get rid of this problem! But - as usual - I am quite lazy, so I need someone to do this for me...



Task & Rules



Given a binary matrix representing the image, output the dimensions (width and height) of the smallest sub-matrix that contains all the $1$s in the original matrix. A sub-matrix is a block of adjacent entries from the original matrix. Equivalently, it is a new matrix formed by overlapping a subset of adjacent rows and a subset of adjacent columns of the original.



  • It is allowed to take the width and the height of the matrix as input as well.

  • The input is guaranteed to contain at least one $1$.

  • You can take input and provide output through any standard method, while taking note that these loopholes are forbidden by default. This is code-golf, so try to complete the task in the least bytes you can manage in your language of choice.

Example



$$left[beginmatrix
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0\
colorred0&colorblue1&colorblue0&colorblue1&colorblue0&colorblue0\
colorred0&colorblue1&colorblue1&colorblue0&colorblue1&colorblue1\
colorred0&colorblue0&colorblue1&colorblue0&colorblue1&colorblue0\
colorred0&colorred0&colorred0&colorred0&colorred0&colorred0endmatrixright] longrightarrow left[beginmatrix1&0&1&0&0\1&1&0&1&1\0&1&0&1&0endmatrixright]longrightarrow(5,3)$$



Test cases




Input | Output

[[0,1,0,0,0,1,0]]
--> (5,1) or (1,5)

[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0]]
--> (3,2) or (2,3)

[[1,1,1,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
--> (4,4)

[[0,0,0,0,0,0],[0,1,0,1,0,1],[0,0,0,0,0,0]]
--> (5,1) or (1,5)

[[0,0,0,0,0],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[0,0,0,0,0]]
--> (3,3)

[[0,0,0,0,0,0],[0,1,0,1,0,0],[0,1,1,0,1,1],[0,0,1,0,1,0],[0,0,0,0,0,0]]
--> (5,3) or (3,5)






code-golf array-manipulation matrix binary-matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 55 mins ago

























asked 1 hour ago









Mr. Xcoder

31k758195




31k758195







  • 1




    This feels very familiar; was it in the Sandbox for a while?
    – Shaggy
    29 mins ago






  • 2




    Possible duplicate of Remove surrounding zeroes of a 2d array.
    – Neil
    27 mins ago










  • A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
    – Shaggy
    13 mins ago












  • 1




    This feels very familiar; was it in the Sandbox for a while?
    – Shaggy
    29 mins ago






  • 2




    Possible duplicate of Remove surrounding zeroes of a 2d array.
    – Neil
    27 mins ago










  • A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
    – Shaggy
    13 mins ago







1




1




This feels very familiar; was it in the Sandbox for a while?
– Shaggy
29 mins ago




This feels very familiar; was it in the Sandbox for a while?
– Shaggy
29 mins ago




2




2




Possible duplicate of Remove surrounding zeroes of a 2d array.
– Neil
27 mins ago




Possible duplicate of Remove surrounding zeroes of a 2d array.
– Neil
27 mins ago












A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
– Shaggy
13 mins ago




A-ha! That's the one I was thinking of, thanks, @Neil. Don't if it's worthy of a dupe-hammer, though - the solution I just posted here definitely couldn't be trivially modified to work for the other challenge. (Unless I'm missing a trick, of course, which is entirely possible.)
– Shaggy
13 mins ago










3 Answers
3






active

oldest

votes

















up vote
2
down vote














Octave, 57 56 bytes



Here find does the heavy lifting: It finds the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.



Thanks @beaker and @AndrasDeak for -1 byte!





@(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2


Try it online!






share|improve this answer





























    up vote
    0
    down vote














    Retina 0.8.2, 83 bytes



    +`^0+¶|¶0+$

    +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
    $1$3$4$5
    (¶?)*0*(.*1)0*
    $#1 $.2


    Try it online! Explanation:



    +`^0+¶|¶0+$


    Delete leading and trailing zero rows.



    +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
    $1$3$4$5


    Remove all 0s on the lines above the last. Remove all the 1s two, but change the digit underneath on the next line to a 1 in that case. This bitwise or's the rows together.



    (¶?)*0*(.*1)0*
    $#1 $.2


    Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.






    share|improve this answer



























      up vote
      0
      down vote













      Japt, 16 bytes



      [UUy]
      ®=ðd)ÌaZÎÉ


      Try it




      Explanation



      [ ] :Create an array containing
      U : The input and
      Uy : The input transposed
      n :Reassign to U
      ® :Map each Z
      ð : Indices of elements where
      d : Any element is truthy (not zero)
      = ) : Reassign to Z
      Ì : Last element
      a : Absolute difference with
      ZÎ : First element
      É : Minus 1





      share|improve this answer






















        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
        );
        );
        , "mathjax-editing");

        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: "200"
        ;
        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: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174832%2ftrim-that-distracting-background-off%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote














        Octave, 57 56 bytes



        Here find does the heavy lifting: It finds the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.



        Thanks @beaker and @AndrasDeak for -1 byte!





        @(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2


        Try it online!






        share|improve this answer


























          up vote
          2
          down vote














          Octave, 57 56 bytes



          Here find does the heavy lifting: It finds the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.



          Thanks @beaker and @AndrasDeak for -1 byte!





          @(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2


          Try it online!






          share|improve this answer
























            up vote
            2
            down vote










            up vote
            2
            down vote










            Octave, 57 56 bytes



            Here find does the heavy lifting: It finds the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.



            Thanks @beaker and @AndrasDeak for -1 byte!





            @(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2


            Try it online!






            share|improve this answer















            Octave, 57 56 bytes



            Here find does the heavy lifting: It finds the row and colum indices of the nonzero entries. Then we just have to find the difference between the maximum and the minimum (plus one) for each of those seperately.



            Thanks @beaker and @AndrasDeak for -1 byte!





            @(a)[I1:2]=find(a),cellfun(@(x)max(x)-min(x)+1,I)2


            Try it online!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 50 mins ago

























            answered 1 hour ago









            flawr

            25.8k562178




            25.8k562178




















                up vote
                0
                down vote














                Retina 0.8.2, 83 bytes



                +`^0+¶|¶0+$

                +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
                $1$3$4$5
                (¶?)*0*(.*1)0*
                $#1 $.2


                Try it online! Explanation:



                +`^0+¶|¶0+$


                Delete leading and trailing zero rows.



                +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
                $1$3$4$5


                Remove all 0s on the lines above the last. Remove all the 1s two, but change the digit underneath on the next line to a 1 in that case. This bitwise or's the rows together.



                (¶?)*0*(.*1)0*
                $#1 $.2


                Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.






                share|improve this answer
























                  up vote
                  0
                  down vote














                  Retina 0.8.2, 83 bytes



                  +`^0+¶|¶0+$

                  +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
                  $1$3$4$5
                  (¶?)*0*(.*1)0*
                  $#1 $.2


                  Try it online! Explanation:



                  +`^0+¶|¶0+$


                  Delete leading and trailing zero rows.



                  +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
                  $1$3$4$5


                  Remove all 0s on the lines above the last. Remove all the 1s two, but change the digit underneath on the next line to a 1 in that case. This bitwise or's the rows together.



                  (¶?)*0*(.*1)0*
                  $#1 $.2


                  Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote










                    Retina 0.8.2, 83 bytes



                    +`^0+¶|¶0+$

                    +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
                    $1$3$4$5
                    (¶?)*0*(.*1)0*
                    $#1 $.2


                    Try it online! Explanation:



                    +`^0+¶|¶0+$


                    Delete leading and trailing zero rows.



                    +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
                    $1$3$4$5


                    Remove all 0s on the lines above the last. Remove all the 1s two, but change the digit underneath on the next line to a 1 in that case. This bitwise or's the rows together.



                    (¶?)*0*(.*1)0*
                    $#1 $.2


                    Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.






                    share|improve this answer













                    Retina 0.8.2, 83 bytes



                    +`^0+¶|¶0+$

                    +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
                    $1$3$4$5
                    (¶?)*0*(.*1)0*
                    $#1 $.2


                    Try it online! Explanation:



                    +`^0+¶|¶0+$


                    Delete leading and trailing zero rows.



                    +1`((.)*).(¶(?<-2>.)*).(?<=(1)¶.*|(.))
                    $1$3$4$5


                    Remove all 0s on the lines above the last. Remove all the 1s two, but change the digit underneath on the next line to a 1 in that case. This bitwise or's the rows together.



                    (¶?)*0*(.*1)0*
                    $#1 $.2


                    Count the number of rows as the number of newlines plus 1 and the number of columns as the number of digits between the first and last 1.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 12 mins ago









                    Neil

                    77k744173




                    77k744173




















                        up vote
                        0
                        down vote













                        Japt, 16 bytes



                        [UUy]
                        ®=ðd)ÌaZÎÉ


                        Try it




                        Explanation



                        [ ] :Create an array containing
                        U : The input and
                        Uy : The input transposed
                        n :Reassign to U
                        ® :Map each Z
                        ð : Indices of elements where
                        d : Any element is truthy (not zero)
                        = ) : Reassign to Z
                        Ì : Last element
                        a : Absolute difference with
                        ZÎ : First element
                        É : Minus 1





                        share|improve this answer


























                          up vote
                          0
                          down vote













                          Japt, 16 bytes



                          [UUy]
                          ®=ðd)ÌaZÎÉ


                          Try it




                          Explanation



                          [ ] :Create an array containing
                          U : The input and
                          Uy : The input transposed
                          n :Reassign to U
                          ® :Map each Z
                          ð : Indices of elements where
                          d : Any element is truthy (not zero)
                          = ) : Reassign to Z
                          Ì : Last element
                          a : Absolute difference with
                          ZÎ : First element
                          É : Minus 1





                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Japt, 16 bytes



                            [UUy]
                            ®=ðd)ÌaZÎÉ


                            Try it




                            Explanation



                            [ ] :Create an array containing
                            U : The input and
                            Uy : The input transposed
                            n :Reassign to U
                            ® :Map each Z
                            ð : Indices of elements where
                            d : Any element is truthy (not zero)
                            = ) : Reassign to Z
                            Ì : Last element
                            a : Absolute difference with
                            ZÎ : First element
                            É : Minus 1





                            share|improve this answer














                            Japt, 16 bytes



                            [UUy]
                            ®=ðd)ÌaZÎÉ


                            Try it




                            Explanation



                            [ ] :Create an array containing
                            U : The input and
                            Uy : The input transposed
                            n :Reassign to U
                            ® :Map each Z
                            ð : Indices of elements where
                            d : Any element is truthy (not zero)
                            = ) : Reassign to Z
                            Ì : Last element
                            a : Absolute difference with
                            ZÎ : First element
                            É : Minus 1






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 45 secs ago

























                            answered 16 mins ago









                            Shaggy

                            17.4k21663




                            17.4k21663



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174832%2ftrim-that-distracting-background-off%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay