Indexing a vector within a bigger one in MATLAB












7















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

























  • 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
    Jan 21 at 17:34


















7















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

























  • 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
    Jan 21 at 17:34
















7












7








7








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
















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















share|improve this question













share|improve this question




share|improve this question








edited Jan 21 at 12:08









UnbearableLightness

4,53241430




4,53241430










asked Jan 21 at 11:39









Gabriele TordiGabriele Tordi

383




383













  • 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
    Jan 21 at 17:34





















  • 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
    Jan 21 at 17:34



















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
Jan 21 at 17:34







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
Jan 21 at 17:34














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
    Jan 21 at 13:26











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

    – Gabriele Tordi
    Jan 21 at 13:29













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

    – barbsan
    Jan 21 at 13:41













  • I've updated code

    – barbsan
    Jan 21 at 13:47



















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
      Jan 21 at 11:59













    • 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
      Jan 21 at 12:18













    • 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
      Jan 21 at 12:42











    • thank you, i am really grateful

      – Gabriele Tordi
      Jan 21 at 13:23



















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


      }
      });














      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
        Jan 21 at 13:26











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

        – Gabriele Tordi
        Jan 21 at 13:29













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

        – barbsan
        Jan 21 at 13:41













      • I've updated code

        – barbsan
        Jan 21 at 13:47
















      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
        Jan 21 at 13:26











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

        – Gabriele Tordi
        Jan 21 at 13:29













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

        – barbsan
        Jan 21 at 13:41













      • I've updated code

        – barbsan
        Jan 21 at 13:47














      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 Jan 21 at 13:47

























      answered Jan 21 at 12:14









      barbsanbarbsan

      2,43621223




      2,43621223













      • thank you, your solution is fastest

        – Gabriele Tordi
        Jan 21 at 13:26











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

        – Gabriele Tordi
        Jan 21 at 13:29













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

        – barbsan
        Jan 21 at 13:41













      • I've updated code

        – barbsan
        Jan 21 at 13:47



















      • thank you, your solution is fastest

        – Gabriele Tordi
        Jan 21 at 13:26











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

        – Gabriele Tordi
        Jan 21 at 13:29













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

        – barbsan
        Jan 21 at 13:41













      • I've updated code

        – barbsan
        Jan 21 at 13:47

















      thank you, your solution is fastest

      – Gabriele Tordi
      Jan 21 at 13:26





      thank you, your solution is fastest

      – Gabriele Tordi
      Jan 21 at 13:26













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

      – Gabriele Tordi
      Jan 21 at 13:29







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

      – Gabriele Tordi
      Jan 21 at 13:29















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

      – barbsan
      Jan 21 at 13:41







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

      – barbsan
      Jan 21 at 13:41















      I've updated code

      – barbsan
      Jan 21 at 13:47





      I've updated code

      – barbsan
      Jan 21 at 13:47













      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 Jan 21 at 13:02









          rahnema1rahnema1

          10.5k2923




          10.5k2923























              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
                Jan 21 at 11:59













              • 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
                Jan 21 at 12:18













              • 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
                Jan 21 at 12:42











              • thank you, i am really grateful

                – Gabriele Tordi
                Jan 21 at 13:23
















              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
                Jan 21 at 11:59













              • 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
                Jan 21 at 12:18













              • 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
                Jan 21 at 12:42











              • thank you, i am really grateful

                – Gabriele Tordi
                Jan 21 at 13:23














              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 Jan 21 at 12:40

























              answered Jan 21 at 11:50









              FlorianFlorian

              1,4792413




              1,4792413













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

                – Gabriele Tordi
                Jan 21 at 11:59













              • 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
                Jan 21 at 12:18













              • 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
                Jan 21 at 12:42











              • thank you, i am really grateful

                – Gabriele Tordi
                Jan 21 at 13:23



















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

                – Gabriele Tordi
                Jan 21 at 11:59













              • 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
                Jan 21 at 12:18













              • 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
                Jan 21 at 12:42











              • thank you, i am really grateful

                – Gabriele Tordi
                Jan 21 at 13:23

















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

              – Gabriele Tordi
              Jan 21 at 11:59







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

              – Gabriele Tordi
              Jan 21 at 11:59















              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
              Jan 21 at 12:18







              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
              Jan 21 at 12:18















              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
              Jan 21 at 12:42





              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
              Jan 21 at 12:42













              thank you, i am really grateful

              – Gabriele Tordi
              Jan 21 at 13:23





              thank you, i am really grateful

              – Gabriele Tordi
              Jan 21 at 13:23











              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 Jan 21 at 15:50

























                  answered Jan 21 at 15:29









                  WolfieWolfie

                  16.1k51744




                  16.1k51744






























                      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%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

                      Human spaceflight

                      Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                      張江高科駅