Indexing a vector within a bigger one in MATLAB












6















I'm trying to find the index position of the smaller vector inside a bigger one.



I've already solved this problem using strfind and bind2dec,
but I don't want to use strfind, I don't want to convert to string or to deciamls at all.



Given the longer vector



a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];


I want to find the index of the smaller vector b inside a



b=[1,1,1,0,0,0];


I would expect to find as result:



result=[15,16,17,18,19,20];


Thank you










share|improve this question









New contributor




Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • What is your solution with strfind? Do you know you can just use strfind(a,b) without converting to strings (but that's undocumented)?

    – Luis Mendo
    2 days ago


















6















I'm trying to find the index position of the smaller vector inside a bigger one.



I've already solved this problem using strfind and bind2dec,
but I don't want to use strfind, I don't want to convert to string or to deciamls at all.



Given the longer vector



a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];


I want to find the index of the smaller vector b inside a



b=[1,1,1,0,0,0];


I would expect to find as result:



result=[15,16,17,18,19,20];


Thank you










share|improve this question









New contributor




Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • What is your solution with strfind? Do you know you can just use strfind(a,b) without converting to strings (but that's undocumented)?

    – Luis Mendo
    2 days ago
















6












6








6








I'm trying to find the index position of the smaller vector inside a bigger one.



I've already solved this problem using strfind and bind2dec,
but I don't want to use strfind, I don't want to convert to string or to deciamls at all.



Given the longer vector



a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];


I want to find the index of the smaller vector b inside a



b=[1,1,1,0,0,0];


I would expect to find as result:



result=[15,16,17,18,19,20];


Thank you










share|improve this question









New contributor




Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'm trying to find the index position of the smaller vector inside a bigger one.



I've already solved this problem using strfind and bind2dec,
but I don't want to use strfind, I don't want to convert to string or to deciamls at all.



Given the longer vector



a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];


I want to find the index of the smaller vector b inside a



b=[1,1,1,0,0,0];


I would expect to find as result:



result=[15,16,17,18,19,20];


Thank you







matlab






share|improve this question









New contributor




Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









UnbearableLightness

4,50741430




4,50741430






New contributor




Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Gabriele TordiGabriele Tordi

333




333




New contributor




Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Gabriele Tordi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • What is your solution with strfind? Do you know you can just use strfind(a,b) without converting to strings (but that's undocumented)?

    – Luis Mendo
    2 days ago





















  • What is your solution with strfind? Do you know you can just use strfind(a,b) without converting to strings (but that's undocumented)?

    – Luis Mendo
    2 days ago



















What is your solution with strfind? Do you know you can just use strfind(a,b) without converting to strings (but that's undocumented)?

– Luis Mendo
2 days ago







What is your solution with strfind? Do you know you can just use strfind(a,b) without converting to strings (but that's undocumented)?

– Luis Mendo
2 days ago














4 Answers
4






active

oldest

votes


















4














Solution with for loops:



a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
b=[1,1,1,0,0,0];
c = ;
b_len = length(b)
maxind0 = length(a) - b_len + 1 %no need to search higher indexes

for i=1:maxind0
found = 0;
for j=1:b_len
if a(i+j-1) == b(j)
found = found + 1;
else
break;
end
end

if found == b_len % if sequence is found fill c with indexes
for j=1:b_len
c(j)= i+j-1;
end

break
end
end

c %display c





share|improve this answer


























  • thank you, your solution is fastest

    – Gabriele Tordi
    2 days ago











  • what about if new_a=a' ? (i transpose a). Also b is transposable

    – Gabriele Tordi
    2 days ago













  • if it's still vector you could replace size(v,2) with length(v)

    – barbsan
    2 days ago













  • I've updated code

    – barbsan
    2 days ago



















7














Here is as solution using 1D convolution. It may find multiple matches so start holds beginning indices of sub-vectors:



f = flip(b);

idx = conv(a,f,'same')==sum(b) & conv(~a,~f,'same')==sum(~b);

start = find(idx)-ceil(length(b)/2)+1;

result = start(1):start(1)+length(b)-1;





share|improve this answer































    3














    Does it need to be computationally efficient?



    A not very efficient but short solution would be this:



    a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];;
    b=[1,1,1,0,0,0];
    where = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));


    ... gives you 15. Your result would be the vector where:where+length(b)-1.



    edit: I tried it and I stand corrected. Here is a version with loops:



    function where = find_sequence(a,b)

    na = 0;
    where = ;
    while na < length(a)-length(b)
    c = false;
    for nb = 1:length(b)
    if a(na+nb)~=b(nb)
    na = na + 1; % + nb
    c = true;
    break
    end

    end
    if ~c
    where = [where,na+1];
    na = na + 1;
    end
    end


    Despite its loops and their bad reputation in Matlab, it's a lot faster:



    a = round(rand(1e6,1));
    b = round(rand(10,1));

    tic;where1 = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));toc;
    tic;where2 = find_sequence(a,b);toc;


    >> test_find_sequence
    Elapsed time is 4.419223 seconds.
    Elapsed time is 0.042969 seconds.





    share|improve this answer


























    • Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work

      – Gabriele Tordi
      2 days ago













    • Try this one and see how it fares for you.It is inefficient for longer vectors b since it will always compare the whole vector while one could stop as soon as there is one mismatch. To fix this, one would have to use loops which are not the most efficient in Matlab either. My gut feeling is that as long as b is very short compared to a it should do okay.

      – Florian
      2 days ago













    • Okay, after testing I found that using the loops is considerably faster. Just saw now after posting my update that barbsan has already suggested a very similar version before I did.

      – Florian
      2 days ago











    • thank you, i am really grateful

      – Gabriele Tordi
      2 days ago



















    2














    A neater method using for would look like this, there is no need for an inner checking loop as we can vectorize that with all...



    a = [1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
    b = [1,1,1,0,0,0];

    idx = NaN( size(b) ); % Output NaNs if not found
    nb = numel( b ); % Store this for re-use

    for ii = 1:numel(a)-nb+1
    if all( a(ii:ii+nb-1) == b )
    % If matched, update the index and exit the loop
    idx = ii:ii+nb-1;
    break
    end
    end


    Output:



    idx = [15,16,17,18,19,20]


    Note, I find this a bit easier to read that some of the nested solutions, but it's not necessarily faster, since the comparison is done on all elements in b each time.






    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
      });


      }
      });






      Gabriele Tordi is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54289194%2findexing-a-vector-within-a-bigger-one-in-matlab%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Solution with for loops:



      a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
      b=[1,1,1,0,0,0];
      c = ;
      b_len = length(b)
      maxind0 = length(a) - b_len + 1 %no need to search higher indexes

      for i=1:maxind0
      found = 0;
      for j=1:b_len
      if a(i+j-1) == b(j)
      found = found + 1;
      else
      break;
      end
      end

      if found == b_len % if sequence is found fill c with indexes
      for j=1:b_len
      c(j)= i+j-1;
      end

      break
      end
      end

      c %display c





      share|improve this answer


























      • thank you, your solution is fastest

        – Gabriele Tordi
        2 days ago











      • what about if new_a=a' ? (i transpose a). Also b is transposable

        – Gabriele Tordi
        2 days ago













      • if it's still vector you could replace size(v,2) with length(v)

        – barbsan
        2 days ago













      • I've updated code

        – barbsan
        2 days ago
















      4














      Solution with for loops:



      a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
      b=[1,1,1,0,0,0];
      c = ;
      b_len = length(b)
      maxind0 = length(a) - b_len + 1 %no need to search higher indexes

      for i=1:maxind0
      found = 0;
      for j=1:b_len
      if a(i+j-1) == b(j)
      found = found + 1;
      else
      break;
      end
      end

      if found == b_len % if sequence is found fill c with indexes
      for j=1:b_len
      c(j)= i+j-1;
      end

      break
      end
      end

      c %display c





      share|improve this answer


























      • thank you, your solution is fastest

        – Gabriele Tordi
        2 days ago











      • what about if new_a=a' ? (i transpose a). Also b is transposable

        – Gabriele Tordi
        2 days ago













      • if it's still vector you could replace size(v,2) with length(v)

        – barbsan
        2 days ago













      • I've updated code

        – barbsan
        2 days ago














      4












      4








      4







      Solution with for loops:



      a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
      b=[1,1,1,0,0,0];
      c = ;
      b_len = length(b)
      maxind0 = length(a) - b_len + 1 %no need to search higher indexes

      for i=1:maxind0
      found = 0;
      for j=1:b_len
      if a(i+j-1) == b(j)
      found = found + 1;
      else
      break;
      end
      end

      if found == b_len % if sequence is found fill c with indexes
      for j=1:b_len
      c(j)= i+j-1;
      end

      break
      end
      end

      c %display c





      share|improve this answer















      Solution with for loops:



      a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
      b=[1,1,1,0,0,0];
      c = ;
      b_len = length(b)
      maxind0 = length(a) - b_len + 1 %no need to search higher indexes

      for i=1:maxind0
      found = 0;
      for j=1:b_len
      if a(i+j-1) == b(j)
      found = found + 1;
      else
      break;
      end
      end

      if found == b_len % if sequence is found fill c with indexes
      for j=1:b_len
      c(j)= i+j-1;
      end

      break
      end
      end

      c %display c






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 days ago

























      answered 2 days ago









      barbsanbarbsan

      2,38321222




      2,38321222













      • thank you, your solution is fastest

        – Gabriele Tordi
        2 days ago











      • what about if new_a=a' ? (i transpose a). Also b is transposable

        – Gabriele Tordi
        2 days ago













      • if it's still vector you could replace size(v,2) with length(v)

        – barbsan
        2 days ago













      • I've updated code

        – barbsan
        2 days ago



















      • thank you, your solution is fastest

        – Gabriele Tordi
        2 days ago











      • what about if new_a=a' ? (i transpose a). Also b is transposable

        – Gabriele Tordi
        2 days ago













      • if it's still vector you could replace size(v,2) with length(v)

        – barbsan
        2 days ago













      • I've updated code

        – barbsan
        2 days ago

















      thank you, your solution is fastest

      – Gabriele Tordi
      2 days ago





      thank you, your solution is fastest

      – Gabriele Tordi
      2 days ago













      what about if new_a=a' ? (i transpose a). Also b is transposable

      – Gabriele Tordi
      2 days ago







      what about if new_a=a' ? (i transpose a). Also b is transposable

      – Gabriele Tordi
      2 days ago















      if it's still vector you could replace size(v,2) with length(v)

      – barbsan
      2 days ago







      if it's still vector you could replace size(v,2) with length(v)

      – barbsan
      2 days ago















      I've updated code

      – barbsan
      2 days ago





      I've updated code

      – barbsan
      2 days ago













      7














      Here is as solution using 1D convolution. It may find multiple matches so start holds beginning indices of sub-vectors:



      f = flip(b);

      idx = conv(a,f,'same')==sum(b) & conv(~a,~f,'same')==sum(~b);

      start = find(idx)-ceil(length(b)/2)+1;

      result = start(1):start(1)+length(b)-1;





      share|improve this answer




























        7














        Here is as solution using 1D convolution. It may find multiple matches so start holds beginning indices of sub-vectors:



        f = flip(b);

        idx = conv(a,f,'same')==sum(b) & conv(~a,~f,'same')==sum(~b);

        start = find(idx)-ceil(length(b)/2)+1;

        result = start(1):start(1)+length(b)-1;





        share|improve this answer


























          7












          7








          7







          Here is as solution using 1D convolution. It may find multiple matches so start holds beginning indices of sub-vectors:



          f = flip(b);

          idx = conv(a,f,'same')==sum(b) & conv(~a,~f,'same')==sum(~b);

          start = find(idx)-ceil(length(b)/2)+1;

          result = start(1):start(1)+length(b)-1;





          share|improve this answer













          Here is as solution using 1D convolution. It may find multiple matches so start holds beginning indices of sub-vectors:



          f = flip(b);

          idx = conv(a,f,'same')==sum(b) & conv(~a,~f,'same')==sum(~b);

          start = find(idx)-ceil(length(b)/2)+1;

          result = start(1):start(1)+length(b)-1;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          rahnema1rahnema1

          10.3k2923




          10.3k2923























              3














              Does it need to be computationally efficient?



              A not very efficient but short solution would be this:



              a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];;
              b=[1,1,1,0,0,0];
              where = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));


              ... gives you 15. Your result would be the vector where:where+length(b)-1.



              edit: I tried it and I stand corrected. Here is a version with loops:



              function where = find_sequence(a,b)

              na = 0;
              where = ;
              while na < length(a)-length(b)
              c = false;
              for nb = 1:length(b)
              if a(na+nb)~=b(nb)
              na = na + 1; % + nb
              c = true;
              break
              end

              end
              if ~c
              where = [where,na+1];
              na = na + 1;
              end
              end


              Despite its loops and their bad reputation in Matlab, it's a lot faster:



              a = round(rand(1e6,1));
              b = round(rand(10,1));

              tic;where1 = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));toc;
              tic;where2 = find_sequence(a,b);toc;


              >> test_find_sequence
              Elapsed time is 4.419223 seconds.
              Elapsed time is 0.042969 seconds.





              share|improve this answer


























              • Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work

                – Gabriele Tordi
                2 days ago













              • Try this one and see how it fares for you.It is inefficient for longer vectors b since it will always compare the whole vector while one could stop as soon as there is one mismatch. To fix this, one would have to use loops which are not the most efficient in Matlab either. My gut feeling is that as long as b is very short compared to a it should do okay.

                – Florian
                2 days ago













              • Okay, after testing I found that using the loops is considerably faster. Just saw now after posting my update that barbsan has already suggested a very similar version before I did.

                – Florian
                2 days ago











              • thank you, i am really grateful

                – Gabriele Tordi
                2 days ago
















              3














              Does it need to be computationally efficient?



              A not very efficient but short solution would be this:



              a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];;
              b=[1,1,1,0,0,0];
              where = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));


              ... gives you 15. Your result would be the vector where:where+length(b)-1.



              edit: I tried it and I stand corrected. Here is a version with loops:



              function where = find_sequence(a,b)

              na = 0;
              where = ;
              while na < length(a)-length(b)
              c = false;
              for nb = 1:length(b)
              if a(na+nb)~=b(nb)
              na = na + 1; % + nb
              c = true;
              break
              end

              end
              if ~c
              where = [where,na+1];
              na = na + 1;
              end
              end


              Despite its loops and their bad reputation in Matlab, it's a lot faster:



              a = round(rand(1e6,1));
              b = round(rand(10,1));

              tic;where1 = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));toc;
              tic;where2 = find_sequence(a,b);toc;


              >> test_find_sequence
              Elapsed time is 4.419223 seconds.
              Elapsed time is 0.042969 seconds.





              share|improve this answer


























              • Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work

                – Gabriele Tordi
                2 days ago













              • Try this one and see how it fares for you.It is inefficient for longer vectors b since it will always compare the whole vector while one could stop as soon as there is one mismatch. To fix this, one would have to use loops which are not the most efficient in Matlab either. My gut feeling is that as long as b is very short compared to a it should do okay.

                – Florian
                2 days ago













              • Okay, after testing I found that using the loops is considerably faster. Just saw now after posting my update that barbsan has already suggested a very similar version before I did.

                – Florian
                2 days ago











              • thank you, i am really grateful

                – Gabriele Tordi
                2 days ago














              3












              3








              3







              Does it need to be computationally efficient?



              A not very efficient but short solution would be this:



              a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];;
              b=[1,1,1,0,0,0];
              where = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));


              ... gives you 15. Your result would be the vector where:where+length(b)-1.



              edit: I tried it and I stand corrected. Here is a version with loops:



              function where = find_sequence(a,b)

              na = 0;
              where = ;
              while na < length(a)-length(b)
              c = false;
              for nb = 1:length(b)
              if a(na+nb)~=b(nb)
              na = na + 1; % + nb
              c = true;
              break
              end

              end
              if ~c
              where = [where,na+1];
              na = na + 1;
              end
              end


              Despite its loops and their bad reputation in Matlab, it's a lot faster:



              a = round(rand(1e6,1));
              b = round(rand(10,1));

              tic;where1 = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));toc;
              tic;where2 = find_sequence(a,b);toc;


              >> test_find_sequence
              Elapsed time is 4.419223 seconds.
              Elapsed time is 0.042969 seconds.





              share|improve this answer















              Does it need to be computationally efficient?



              A not very efficient but short solution would be this:



              a=[1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];;
              b=[1,1,1,0,0,0];
              where = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));


              ... gives you 15. Your result would be the vector where:where+length(b)-1.



              edit: I tried it and I stand corrected. Here is a version with loops:



              function where = find_sequence(a,b)

              na = 0;
              where = ;
              while na < length(a)-length(b)
              c = false;
              for nb = 1:length(b)
              if a(na+nb)~=b(nb)
              na = na + 1; % + nb
              c = true;
              break
              end

              end
              if ~c
              where = [where,na+1];
              na = na + 1;
              end
              end


              Despite its loops and their bad reputation in Matlab, it's a lot faster:



              a = round(rand(1e6,1));
              b = round(rand(10,1));

              tic;where1 = find(arrayfun(@(n) all(a(n+1:n+length(b))==b),0:length(a)-length(b)));toc;
              tic;where2 = find_sequence(a,b);toc;


              >> test_find_sequence
              Elapsed time is 4.419223 seconds.
              Elapsed time is 0.042969 seconds.






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 days ago

























              answered 2 days ago









              FlorianFlorian

              1,3492412




              1,3492412













              • Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work

                – Gabriele Tordi
                2 days ago













              • Try this one and see how it fares for you.It is inefficient for longer vectors b since it will always compare the whole vector while one could stop as soon as there is one mismatch. To fix this, one would have to use loops which are not the most efficient in Matlab either. My gut feeling is that as long as b is very short compared to a it should do okay.

                – Florian
                2 days ago













              • Okay, after testing I found that using the loops is considerably faster. Just saw now after posting my update that barbsan has already suggested a very similar version before I did.

                – Florian
                2 days ago











              • thank you, i am really grateful

                – Gabriele Tordi
                2 days ago



















              • Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work

                – Gabriele Tordi
                2 days ago













              • Try this one and see how it fares for you.It is inefficient for longer vectors b since it will always compare the whole vector while one could stop as soon as there is one mismatch. To fix this, one would have to use loops which are not the most efficient in Matlab either. My gut feeling is that as long as b is very short compared to a it should do okay.

                – Florian
                2 days ago













              • Okay, after testing I found that using the loops is considerably faster. Just saw now after posting my update that barbsan has already suggested a very similar version before I did.

                – Florian
                2 days ago











              • thank you, i am really grateful

                – Gabriele Tordi
                2 days ago

















              Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work

              – Gabriele Tordi
              2 days ago







              Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work

              – Gabriele Tordi
              2 days ago















              Try this one and see how it fares for you.It is inefficient for longer vectors b since it will always compare the whole vector while one could stop as soon as there is one mismatch. To fix this, one would have to use loops which are not the most efficient in Matlab either. My gut feeling is that as long as b is very short compared to a it should do okay.

              – Florian
              2 days ago







              Try this one and see how it fares for you.It is inefficient for longer vectors b since it will always compare the whole vector while one could stop as soon as there is one mismatch. To fix this, one would have to use loops which are not the most efficient in Matlab either. My gut feeling is that as long as b is very short compared to a it should do okay.

              – Florian
              2 days ago















              Okay, after testing I found that using the loops is considerably faster. Just saw now after posting my update that barbsan has already suggested a very similar version before I did.

              – Florian
              2 days ago





              Okay, after testing I found that using the loops is considerably faster. Just saw now after posting my update that barbsan has already suggested a very similar version before I did.

              – Florian
              2 days ago













              thank you, i am really grateful

              – Gabriele Tordi
              2 days ago





              thank you, i am really grateful

              – Gabriele Tordi
              2 days ago











              2














              A neater method using for would look like this, there is no need for an inner checking loop as we can vectorize that with all...



              a = [1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
              b = [1,1,1,0,0,0];

              idx = NaN( size(b) ); % Output NaNs if not found
              nb = numel( b ); % Store this for re-use

              for ii = 1:numel(a)-nb+1
              if all( a(ii:ii+nb-1) == b )
              % If matched, update the index and exit the loop
              idx = ii:ii+nb-1;
              break
              end
              end


              Output:



              idx = [15,16,17,18,19,20]


              Note, I find this a bit easier to read that some of the nested solutions, but it's not necessarily faster, since the comparison is done on all elements in b each time.






              share|improve this answer






























                2














                A neater method using for would look like this, there is no need for an inner checking loop as we can vectorize that with all...



                a = [1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
                b = [1,1,1,0,0,0];

                idx = NaN( size(b) ); % Output NaNs if not found
                nb = numel( b ); % Store this for re-use

                for ii = 1:numel(a)-nb+1
                if all( a(ii:ii+nb-1) == b )
                % If matched, update the index and exit the loop
                idx = ii:ii+nb-1;
                break
                end
                end


                Output:



                idx = [15,16,17,18,19,20]


                Note, I find this a bit easier to read that some of the nested solutions, but it's not necessarily faster, since the comparison is done on all elements in b each time.






                share|improve this answer




























                  2












                  2








                  2







                  A neater method using for would look like this, there is no need for an inner checking loop as we can vectorize that with all...



                  a = [1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
                  b = [1,1,1,0,0,0];

                  idx = NaN( size(b) ); % Output NaNs if not found
                  nb = numel( b ); % Store this for re-use

                  for ii = 1:numel(a)-nb+1
                  if all( a(ii:ii+nb-1) == b )
                  % If matched, update the index and exit the loop
                  idx = ii:ii+nb-1;
                  break
                  end
                  end


                  Output:



                  idx = [15,16,17,18,19,20]


                  Note, I find this a bit easier to read that some of the nested solutions, but it's not necessarily faster, since the comparison is done on all elements in b each time.






                  share|improve this answer















                  A neater method using for would look like this, there is no need for an inner checking loop as we can vectorize that with all...



                  a = [1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1];
                  b = [1,1,1,0,0,0];

                  idx = NaN( size(b) ); % Output NaNs if not found
                  nb = numel( b ); % Store this for re-use

                  for ii = 1:numel(a)-nb+1
                  if all( a(ii:ii+nb-1) == b )
                  % If matched, update the index and exit the loop
                  idx = ii:ii+nb-1;
                  break
                  end
                  end


                  Output:



                  idx = [15,16,17,18,19,20]


                  Note, I find this a bit easier to read that some of the nested solutions, but it's not necessarily faster, since the comparison is done on all elements in b each time.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago

























                  answered 2 days ago









                  WolfieWolfie

                  15.7k51744




                  15.7k51744






















                      Gabriele Tordi is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Gabriele Tordi is a new contributor. Be nice, and check out our Code of Conduct.













                      Gabriele Tordi is a new contributor. Be nice, and check out our Code of Conduct.












                      Gabriele Tordi is a new contributor. Be nice, and check out our Code of Conduct.
















                      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%2f54289194%2findexing-a-vector-within-a-bigger-one-in-matlab%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

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]