Find index of nth occurence of 1 in binary vector (matlab)
up vote
1
down vote
favorite
I have a binary vector like this x = [0 0 1 1 1 1 1 1 1]
. I want to find the index of lets say the 7th 1, which is 9.
I know I can do this:
y = find(x);
index = y(7);
But what if the vector is huge and I want to conserve memory usage? Wouldn't y = find(x)
use alot of memory? If so, is there any way around this?
I am using this as an alternate way of storing indexes for nonbasis and basis elements in a linear programming problem. So I would like to avoid storing the indices as numerical values.
Would the following be a good solution?
basis = [0 0 1 1 1 1 1 1 1];
basisIndex = 7;
correctIndex = getIndex(basisIndex, basis); % should be 9 when basisIndex = 7
function ret = getIndex(basisIndex, basis)
counter = 1;
for value = find(basis) % iterate through [3, 4, 5, 6, 7, 8, 9]
if counter == basisIndex
ret = value;
break;
end
counter = counter + 1;
end
end
matlab vector indexing binary
add a comment |
up vote
1
down vote
favorite
I have a binary vector like this x = [0 0 1 1 1 1 1 1 1]
. I want to find the index of lets say the 7th 1, which is 9.
I know I can do this:
y = find(x);
index = y(7);
But what if the vector is huge and I want to conserve memory usage? Wouldn't y = find(x)
use alot of memory? If so, is there any way around this?
I am using this as an alternate way of storing indexes for nonbasis and basis elements in a linear programming problem. So I would like to avoid storing the indices as numerical values.
Would the following be a good solution?
basis = [0 0 1 1 1 1 1 1 1];
basisIndex = 7;
correctIndex = getIndex(basisIndex, basis); % should be 9 when basisIndex = 7
function ret = getIndex(basisIndex, basis)
counter = 1;
for value = find(basis) % iterate through [3, 4, 5, 6, 7, 8, 9]
if counter == basisIndex
ret = value;
break;
end
counter = counter + 1;
end
end
matlab vector indexing binary
I added some information at the end of the question. I was thinking that saving the values returned by find would take away the advantage of storing the indices in a binary vector.
– StraightUpBusta
Nov 10 at 9:41
@SardarUsama I was thinking that the function I added would conserve memory?
– StraightUpBusta
Nov 10 at 10:17
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a binary vector like this x = [0 0 1 1 1 1 1 1 1]
. I want to find the index of lets say the 7th 1, which is 9.
I know I can do this:
y = find(x);
index = y(7);
But what if the vector is huge and I want to conserve memory usage? Wouldn't y = find(x)
use alot of memory? If so, is there any way around this?
I am using this as an alternate way of storing indexes for nonbasis and basis elements in a linear programming problem. So I would like to avoid storing the indices as numerical values.
Would the following be a good solution?
basis = [0 0 1 1 1 1 1 1 1];
basisIndex = 7;
correctIndex = getIndex(basisIndex, basis); % should be 9 when basisIndex = 7
function ret = getIndex(basisIndex, basis)
counter = 1;
for value = find(basis) % iterate through [3, 4, 5, 6, 7, 8, 9]
if counter == basisIndex
ret = value;
break;
end
counter = counter + 1;
end
end
matlab vector indexing binary
I have a binary vector like this x = [0 0 1 1 1 1 1 1 1]
. I want to find the index of lets say the 7th 1, which is 9.
I know I can do this:
y = find(x);
index = y(7);
But what if the vector is huge and I want to conserve memory usage? Wouldn't y = find(x)
use alot of memory? If so, is there any way around this?
I am using this as an alternate way of storing indexes for nonbasis and basis elements in a linear programming problem. So I would like to avoid storing the indices as numerical values.
Would the following be a good solution?
basis = [0 0 1 1 1 1 1 1 1];
basisIndex = 7;
correctIndex = getIndex(basisIndex, basis); % should be 9 when basisIndex = 7
function ret = getIndex(basisIndex, basis)
counter = 1;
for value = find(basis) % iterate through [3, 4, 5, 6, 7, 8, 9]
if counter == basisIndex
ret = value;
break;
end
counter = counter + 1;
end
end
matlab vector indexing binary
matlab vector indexing binary
edited Nov 10 at 10:16
asked Nov 10 at 9:30
StraightUpBusta
396
396
I added some information at the end of the question. I was thinking that saving the values returned by find would take away the advantage of storing the indices in a binary vector.
– StraightUpBusta
Nov 10 at 9:41
@SardarUsama I was thinking that the function I added would conserve memory?
– StraightUpBusta
Nov 10 at 10:17
add a comment |
I added some information at the end of the question. I was thinking that saving the values returned by find would take away the advantage of storing the indices in a binary vector.
– StraightUpBusta
Nov 10 at 9:41
@SardarUsama I was thinking that the function I added would conserve memory?
– StraightUpBusta
Nov 10 at 10:17
I added some information at the end of the question. I was thinking that saving the values returned by find would take away the advantage of storing the indices in a binary vector.
– StraightUpBusta
Nov 10 at 9:41
I added some information at the end of the question. I was thinking that saving the values returned by find would take away the advantage of storing the indices in a binary vector.
– StraightUpBusta
Nov 10 at 9:41
@SardarUsama I was thinking that the function I added would conserve memory?
– StraightUpBusta
Nov 10 at 10:17
@SardarUsama I was thinking that the function I added would conserve memory?
– StraightUpBusta
Nov 10 at 10:17
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Just iterate through x
. First, it will not create a new vector y=find(x)
(save memory). Second, if basisIndex
is small, it will be more efficient.
Suppose x
is a 1e8 by 1 vector. Let's compare find
with just iteration.
basis = randi(2,1e8,1) - 1;
basisIndex = 7;
tic % your first method
y = find(basis);
index = y(basisIndex);
toc
tic % iterate through base
index = 1;
match = 0;
while true
if basis(index)
match = match + 1;
if match == basisIndex
break
end
end
index = index + 1;
end
toc
Output
Elapsed time is 1.214597 seconds.
Elapsed time is 0.000061 seconds.
Even if the basisIndex
is large
basisIndex = 5e7;
The result from iteration is still more efficient
Elapsed time is 1.250430 seconds. % use find
Elapsed time is 0.757767 seconds. % use iteration
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%2f53237630%2ffind-index-of-nth-occurence-of-1-in-binary-vector-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Just iterate through x
. First, it will not create a new vector y=find(x)
(save memory). Second, if basisIndex
is small, it will be more efficient.
Suppose x
is a 1e8 by 1 vector. Let's compare find
with just iteration.
basis = randi(2,1e8,1) - 1;
basisIndex = 7;
tic % your first method
y = find(basis);
index = y(basisIndex);
toc
tic % iterate through base
index = 1;
match = 0;
while true
if basis(index)
match = match + 1;
if match == basisIndex
break
end
end
index = index + 1;
end
toc
Output
Elapsed time is 1.214597 seconds.
Elapsed time is 0.000061 seconds.
Even if the basisIndex
is large
basisIndex = 5e7;
The result from iteration is still more efficient
Elapsed time is 1.250430 seconds. % use find
Elapsed time is 0.757767 seconds. % use iteration
add a comment |
up vote
2
down vote
accepted
Just iterate through x
. First, it will not create a new vector y=find(x)
(save memory). Second, if basisIndex
is small, it will be more efficient.
Suppose x
is a 1e8 by 1 vector. Let's compare find
with just iteration.
basis = randi(2,1e8,1) - 1;
basisIndex = 7;
tic % your first method
y = find(basis);
index = y(basisIndex);
toc
tic % iterate through base
index = 1;
match = 0;
while true
if basis(index)
match = match + 1;
if match == basisIndex
break
end
end
index = index + 1;
end
toc
Output
Elapsed time is 1.214597 seconds.
Elapsed time is 0.000061 seconds.
Even if the basisIndex
is large
basisIndex = 5e7;
The result from iteration is still more efficient
Elapsed time is 1.250430 seconds. % use find
Elapsed time is 0.757767 seconds. % use iteration
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Just iterate through x
. First, it will not create a new vector y=find(x)
(save memory). Second, if basisIndex
is small, it will be more efficient.
Suppose x
is a 1e8 by 1 vector. Let's compare find
with just iteration.
basis = randi(2,1e8,1) - 1;
basisIndex = 7;
tic % your first method
y = find(basis);
index = y(basisIndex);
toc
tic % iterate through base
index = 1;
match = 0;
while true
if basis(index)
match = match + 1;
if match == basisIndex
break
end
end
index = index + 1;
end
toc
Output
Elapsed time is 1.214597 seconds.
Elapsed time is 0.000061 seconds.
Even if the basisIndex
is large
basisIndex = 5e7;
The result from iteration is still more efficient
Elapsed time is 1.250430 seconds. % use find
Elapsed time is 0.757767 seconds. % use iteration
Just iterate through x
. First, it will not create a new vector y=find(x)
(save memory). Second, if basisIndex
is small, it will be more efficient.
Suppose x
is a 1e8 by 1 vector. Let's compare find
with just iteration.
basis = randi(2,1e8,1) - 1;
basisIndex = 7;
tic % your first method
y = find(basis);
index = y(basisIndex);
toc
tic % iterate through base
index = 1;
match = 0;
while true
if basis(index)
match = match + 1;
if match == basisIndex
break
end
end
index = index + 1;
end
toc
Output
Elapsed time is 1.214597 seconds.
Elapsed time is 0.000061 seconds.
Even if the basisIndex
is large
basisIndex = 5e7;
The result from iteration is still more efficient
Elapsed time is 1.250430 seconds. % use find
Elapsed time is 0.757767 seconds. % use iteration
answered Nov 10 at 13:14
Banghua Zhao
1,258719
1,258719
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53237630%2ffind-index-of-nth-occurence-of-1-in-binary-vector-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
I added some information at the end of the question. I was thinking that saving the values returned by find would take away the advantage of storing the indices in a binary vector.
– StraightUpBusta
Nov 10 at 9:41
@SardarUsama I was thinking that the function I added would conserve memory?
– StraightUpBusta
Nov 10 at 10:17