Count values in a vector less than each one of the elements in another vector












4















I have two vectors r and d and I'd like to know the number of times r<d(i) where i=1:length(d).



r=rand(1,1E7);
d=linspace(0,1,10);


So far I've got the following, but it's not very elegant:



for i=1:length(d)
sum(r<d(i))
end


This is an example in R but I'm not really sure this would work for matlab:
Finding number of elements in one vector that are less than an element in another vector










share|improve this question





























    4















    I have two vectors r and d and I'd like to know the number of times r<d(i) where i=1:length(d).



    r=rand(1,1E7);
    d=linspace(0,1,10);


    So far I've got the following, but it's not very elegant:



    for i=1:length(d)
    sum(r<d(i))
    end


    This is an example in R but I'm not really sure this would work for matlab:
    Finding number of elements in one vector that are less than an element in another vector










    share|improve this question



























      4












      4








      4








      I have two vectors r and d and I'd like to know the number of times r<d(i) where i=1:length(d).



      r=rand(1,1E7);
      d=linspace(0,1,10);


      So far I've got the following, but it's not very elegant:



      for i=1:length(d)
      sum(r<d(i))
      end


      This is an example in R but I'm not really sure this would work for matlab:
      Finding number of elements in one vector that are less than an element in another vector










      share|improve this question
















      I have two vectors r and d and I'd like to know the number of times r<d(i) where i=1:length(d).



      r=rand(1,1E7);
      d=linspace(0,1,10);


      So far I've got the following, but it's not very elegant:



      for i=1:length(d)
      sum(r<d(i))
      end


      This is an example in R but I'm not really sure this would work for matlab:
      Finding number of elements in one vector that are less than an element in another vector







      arrays matlab vector






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 16:48







      HCAI

















      asked Nov 16 '18 at 16:26









      HCAIHCAI

      57541338




      57541338
























          3 Answers
          3






          active

          oldest

          votes


















          6














          You can use singleton expansion with bsxfun: faster, more elegant than the loop, but also more memory-intensive:



          result = sum(bsxfun(@lt, r(:), d(:).'), 1);


          In recent Matlab versions bsxfun can be dropped thanks to implicit singleton expansion:



          result = sum(r(:)<d(:).', 1);


          An alternative approach is to use the histcounts function with the 'cumcount' option:



          result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
          result = result(1:end-1);





          share|improve this answer





















          • 1





            aw, 50 seconds late...

            – Brice
            Nov 16 '18 at 16:36











          • @Brice ¯_(ツ)_/¯ :-D

            – Luis Mendo
            Nov 16 '18 at 16:37











          • Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.

            – HCAI
            Nov 16 '18 at 16:46











          • @HCAI The histcounts approach should be better then

            – Luis Mendo
            Nov 16 '18 at 16:47








          • 1





            Thank you very much, I'd like to accept your answer. The histcount is super fast!!!

            – HCAI
            Nov 16 '18 at 16:57



















          3














          You may build a matrix flagging values from vector r inferior to values from vector d in one time with bsxfun, then sum the values:



          flag=bsxfun(@lt,r',d);
          result=sum(flag,1);





          share|improve this answer
























          • Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.

            – HCAI
            Nov 16 '18 at 16:58



















          1














          For each element in d, count how many times this element is bigger than the elements in r, which is equivalent to your problem.



          r=rand(1,10);
          d=linspace(0,1,10);

          result = sum(d>r(:))


          Output:



          result =
          0 0 1 2 7 8 8 8 9 10





          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',
            autoActivateHeartbeat: false,
            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%2f53341823%2fcount-values-in-a-vector-less-than-each-one-of-the-elements-in-another-vector%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            You can use singleton expansion with bsxfun: faster, more elegant than the loop, but also more memory-intensive:



            result = sum(bsxfun(@lt, r(:), d(:).'), 1);


            In recent Matlab versions bsxfun can be dropped thanks to implicit singleton expansion:



            result = sum(r(:)<d(:).', 1);


            An alternative approach is to use the histcounts function with the 'cumcount' option:



            result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
            result = result(1:end-1);





            share|improve this answer





















            • 1





              aw, 50 seconds late...

              – Brice
              Nov 16 '18 at 16:36











            • @Brice ¯_(ツ)_/¯ :-D

              – Luis Mendo
              Nov 16 '18 at 16:37











            • Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.

              – HCAI
              Nov 16 '18 at 16:46











            • @HCAI The histcounts approach should be better then

              – Luis Mendo
              Nov 16 '18 at 16:47








            • 1





              Thank you very much, I'd like to accept your answer. The histcount is super fast!!!

              – HCAI
              Nov 16 '18 at 16:57
















            6














            You can use singleton expansion with bsxfun: faster, more elegant than the loop, but also more memory-intensive:



            result = sum(bsxfun(@lt, r(:), d(:).'), 1);


            In recent Matlab versions bsxfun can be dropped thanks to implicit singleton expansion:



            result = sum(r(:)<d(:).', 1);


            An alternative approach is to use the histcounts function with the 'cumcount' option:



            result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
            result = result(1:end-1);





            share|improve this answer





















            • 1





              aw, 50 seconds late...

              – Brice
              Nov 16 '18 at 16:36











            • @Brice ¯_(ツ)_/¯ :-D

              – Luis Mendo
              Nov 16 '18 at 16:37











            • Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.

              – HCAI
              Nov 16 '18 at 16:46











            • @HCAI The histcounts approach should be better then

              – Luis Mendo
              Nov 16 '18 at 16:47








            • 1





              Thank you very much, I'd like to accept your answer. The histcount is super fast!!!

              – HCAI
              Nov 16 '18 at 16:57














            6












            6








            6







            You can use singleton expansion with bsxfun: faster, more elegant than the loop, but also more memory-intensive:



            result = sum(bsxfun(@lt, r(:), d(:).'), 1);


            In recent Matlab versions bsxfun can be dropped thanks to implicit singleton expansion:



            result = sum(r(:)<d(:).', 1);


            An alternative approach is to use the histcounts function with the 'cumcount' option:



            result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
            result = result(1:end-1);





            share|improve this answer















            You can use singleton expansion with bsxfun: faster, more elegant than the loop, but also more memory-intensive:



            result = sum(bsxfun(@lt, r(:), d(:).'), 1);


            In recent Matlab versions bsxfun can be dropped thanks to implicit singleton expansion:



            result = sum(r(:)<d(:).', 1);


            An alternative approach is to use the histcounts function with the 'cumcount' option:



            result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
            result = result(1:end-1);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 16:48

























            answered Nov 16 '18 at 16:34









            Luis MendoLuis Mendo

            93.2k1154122




            93.2k1154122








            • 1





              aw, 50 seconds late...

              – Brice
              Nov 16 '18 at 16:36











            • @Brice ¯_(ツ)_/¯ :-D

              – Luis Mendo
              Nov 16 '18 at 16:37











            • Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.

              – HCAI
              Nov 16 '18 at 16:46











            • @HCAI The histcounts approach should be better then

              – Luis Mendo
              Nov 16 '18 at 16:47








            • 1





              Thank you very much, I'd like to accept your answer. The histcount is super fast!!!

              – HCAI
              Nov 16 '18 at 16:57














            • 1





              aw, 50 seconds late...

              – Brice
              Nov 16 '18 at 16:36











            • @Brice ¯_(ツ)_/¯ :-D

              – Luis Mendo
              Nov 16 '18 at 16:37











            • Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.

              – HCAI
              Nov 16 '18 at 16:46











            • @HCAI The histcounts approach should be better then

              – Luis Mendo
              Nov 16 '18 at 16:47








            • 1





              Thank you very much, I'd like to accept your answer. The histcount is super fast!!!

              – HCAI
              Nov 16 '18 at 16:57








            1




            1





            aw, 50 seconds late...

            – Brice
            Nov 16 '18 at 16:36





            aw, 50 seconds late...

            – Brice
            Nov 16 '18 at 16:36













            @Brice ¯_(ツ)_/¯ :-D

            – Luis Mendo
            Nov 16 '18 at 16:37





            @Brice ¯_(ツ)_/¯ :-D

            – Luis Mendo
            Nov 16 '18 at 16:37













            Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.

            – HCAI
            Nov 16 '18 at 16:46





            Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.

            – HCAI
            Nov 16 '18 at 16:46













            @HCAI The histcounts approach should be better then

            – Luis Mendo
            Nov 16 '18 at 16:47







            @HCAI The histcounts approach should be better then

            – Luis Mendo
            Nov 16 '18 at 16:47






            1




            1





            Thank you very much, I'd like to accept your answer. The histcount is super fast!!!

            – HCAI
            Nov 16 '18 at 16:57





            Thank you very much, I'd like to accept your answer. The histcount is super fast!!!

            – HCAI
            Nov 16 '18 at 16:57













            3














            You may build a matrix flagging values from vector r inferior to values from vector d in one time with bsxfun, then sum the values:



            flag=bsxfun(@lt,r',d);
            result=sum(flag,1);





            share|improve this answer
























            • Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.

              – HCAI
              Nov 16 '18 at 16:58
















            3














            You may build a matrix flagging values from vector r inferior to values from vector d in one time with bsxfun, then sum the values:



            flag=bsxfun(@lt,r',d);
            result=sum(flag,1);





            share|improve this answer
























            • Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.

              – HCAI
              Nov 16 '18 at 16:58














            3












            3








            3







            You may build a matrix flagging values from vector r inferior to values from vector d in one time with bsxfun, then sum the values:



            flag=bsxfun(@lt,r',d);
            result=sum(flag,1);





            share|improve this answer













            You may build a matrix flagging values from vector r inferior to values from vector d in one time with bsxfun, then sum the values:



            flag=bsxfun(@lt,r',d);
            result=sum(flag,1);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 16:35









            BriceBrice

            1,400110




            1,400110













            • Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.

              – HCAI
              Nov 16 '18 at 16:58



















            • Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.

              – HCAI
              Nov 16 '18 at 16:58

















            Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.

            – HCAI
            Nov 16 '18 at 16:58





            Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.

            – HCAI
            Nov 16 '18 at 16:58











            1














            For each element in d, count how many times this element is bigger than the elements in r, which is equivalent to your problem.



            r=rand(1,10);
            d=linspace(0,1,10);

            result = sum(d>r(:))


            Output:



            result =
            0 0 1 2 7 8 8 8 9 10





            share|improve this answer






























              1














              For each element in d, count how many times this element is bigger than the elements in r, which is equivalent to your problem.



              r=rand(1,10);
              d=linspace(0,1,10);

              result = sum(d>r(:))


              Output:



              result =
              0 0 1 2 7 8 8 8 9 10





              share|improve this answer




























                1












                1








                1







                For each element in d, count how many times this element is bigger than the elements in r, which is equivalent to your problem.



                r=rand(1,10);
                d=linspace(0,1,10);

                result = sum(d>r(:))


                Output:



                result =
                0 0 1 2 7 8 8 8 9 10





                share|improve this answer















                For each element in d, count how many times this element is bigger than the elements in r, which is equivalent to your problem.



                r=rand(1,10);
                d=linspace(0,1,10);

                result = sum(d>r(:))


                Output:



                result =
                0 0 1 2 7 8 8 8 9 10






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 16:54

























                answered Nov 16 '18 at 16:39









                Banghua ZhaoBanghua Zhao

                1,2771719




                1,2771719






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341823%2fcount-values-in-a-vector-less-than-each-one-of-the-elements-in-another-vector%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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()