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
New contributor
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
New contributor
What is your solution withstrfind
? Do you know you can just usestrfind(a,b)
without converting to strings (but that's undocumented)?
– Luis Mendo
1 hour ago
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
New contributor
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
New contributor
New contributor
edited 7 hours ago
UnbearableLightness
4,50741430
4,50741430
New contributor
asked 7 hours ago
Gabriele TordiGabriele Tordi
333
333
New contributor
New contributor
What is your solution withstrfind
? Do you know you can just usestrfind(a,b)
without converting to strings (but that's undocumented)?
– Luis Mendo
1 hour ago
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
1 hour 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
1 hour 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
1 hour ago
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
6 hours ago
what about if new_a=a' ? (i transpose a). Also b is transposable
– Gabriele Tordi
6 hours ago
if it's still vector you could replacesize(v,2)
withlength(v)
– barbsan
5 hours ago
I've updated code
– barbsan
5 hours ago
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
7 hours ago
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
7 hours 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
6 hours ago
thank you, i am really grateful
– Gabriele Tordi
6 hours ago
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
});
}
});
Gabriele Tordi is a new contributor. Be nice, and check out our Code of Conduct.
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
6 hours ago
what about if new_a=a' ? (i transpose a). Also b is transposable
– Gabriele Tordi
6 hours ago
if it's still vector you could replacesize(v,2)
withlength(v)
– barbsan
5 hours ago
I've updated code
– barbsan
5 hours ago
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
6 hours ago
what about if new_a=a' ? (i transpose a). Also b is transposable
– Gabriele Tordi
6 hours ago
if it's still vector you could replacesize(v,2)
withlength(v)
– barbsan
5 hours ago
I've updated code
– barbsan
5 hours ago
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 5 hours ago
answered 7 hours ago
barbsanbarbsan
2,34321222
2,34321222
thank you, your solution is fastest
– Gabriele Tordi
6 hours ago
what about if new_a=a' ? (i transpose a). Also b is transposable
– Gabriele Tordi
6 hours ago
if it's still vector you could replacesize(v,2)
withlength(v)
– barbsan
5 hours ago
I've updated code
– barbsan
5 hours ago
add a comment |
thank you, your solution is fastest
– Gabriele Tordi
6 hours ago
what about if new_a=a' ? (i transpose a). Also b is transposable
– Gabriele Tordi
6 hours ago
if it's still vector you could replacesize(v,2)
withlength(v)
– barbsan
5 hours ago
I've updated code
– barbsan
5 hours ago
thank you, your solution is fastest
– Gabriele Tordi
6 hours ago
thank you, your solution is fastest
– Gabriele Tordi
6 hours ago
what about if new_a=a' ? (i transpose a). Also b is transposable
– Gabriele Tordi
6 hours ago
what about if new_a=a' ? (i transpose a). Also b is transposable
– Gabriele Tordi
6 hours ago
if it's still vector you could replace
size(v,2)
with length(v)
– barbsan
5 hours ago
if it's still vector you could replace
size(v,2)
with length(v)
– barbsan
5 hours ago
I've updated code
– barbsan
5 hours ago
I've updated code
– barbsan
5 hours ago
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 6 hours ago
rahnema1rahnema1
10.3k2923
10.3k2923
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
7 hours ago
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
7 hours 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
6 hours ago
thank you, i am really grateful
– Gabriele Tordi
6 hours ago
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
7 hours ago
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
7 hours 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
6 hours ago
thank you, i am really grateful
– Gabriele Tordi
6 hours ago
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 6 hours ago
answered 7 hours 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
7 hours ago
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
7 hours 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
6 hours ago
thank you, i am really grateful
– Gabriele Tordi
6 hours ago
add a comment |
Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work
– Gabriele Tordi
7 hours ago
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
7 hours 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
6 hours ago
thank you, i am really grateful
– Gabriele Tordi
6 hours ago
Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work
– Gabriele Tordi
7 hours ago
Yes, efficency is important. Also I am asking because strfind and bin2dec require to much memory to work
– Gabriele Tordi
7 hours 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
7 hours 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
7 hours 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
6 hours 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
6 hours ago
thank you, i am really grateful
– Gabriele Tordi
6 hours ago
thank you, i am really grateful
– Gabriele Tordi
6 hours ago
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 3 hours ago
answered 4 hours ago
WolfieWolfie
15.7k51744
15.7k51744
add a comment |
add a comment |
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.
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.
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
1 hour ago