Indexing a vector within a bigger one in MATLAB
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
add a comment |
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
What is your solution withstrfind
? Do you know you can just usestrfind(a,b)
without converting to strings (but that's undocumented)?
– Luis Mendo
Jan 21 at 17:34
add a comment |
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
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
matlab
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 withstrfind
? Do you know you can just usestrfind(a,b)
without converting to strings (but that's undocumented)?
– Luis Mendo
Jan 21 at 17:34
add a comment |
What is your solution withstrfind
? Do you know you can just usestrfind(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
add a comment |
4 Answers
4
active
oldest
votes
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
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 replacesize(v,2)
withlength(v)
– barbsan
Jan 21 at 13:41
I've updated code
– barbsan
Jan 21 at 13:47
add a comment |
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;
add a comment |
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.
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 vectorsb
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 asb
is very short compared toa
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
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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 replacesize(v,2)
withlength(v)
– barbsan
Jan 21 at 13:41
I've updated code
– barbsan
Jan 21 at 13:47
add a comment |
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
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 replacesize(v,2)
withlength(v)
– barbsan
Jan 21 at 13:41
I've updated code
– barbsan
Jan 21 at 13:47
add a comment |
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
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
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 replacesize(v,2)
withlength(v)
– barbsan
Jan 21 at 13:41
I've updated code
– barbsan
Jan 21 at 13:47
add a comment |
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 replacesize(v,2)
withlength(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
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Jan 21 at 13:02
rahnema1rahnema1
10.5k2923
10.5k2923
add a comment |
add a comment |
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.
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 vectorsb
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 asb
is very short compared toa
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
add a comment |
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.
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 vectorsb
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 asb
is very short compared toa
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
add a comment |
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.
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.
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 vectorsb
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 asb
is very short compared toa
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
add a comment |
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 vectorsb
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 asb
is very short compared toa
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 21 at 15:50
answered Jan 21 at 15:29
WolfieWolfie
16.1k51744
16.1k51744
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What is your solution with
strfind
? Do you know you can just usestrfind(a,b)
without converting to strings (but that's undocumented)?– Luis Mendo
Jan 21 at 17:34