How to select specific percentage of items from a vector in matlab?
up vote
0
down vote
favorite
I have used datasample()
and randsample()
to select specific no. of items randomly from a vector. But I have to specify the no. of items explicitly. Is there any way to specify percentage instead?
matlab
add a comment |
up vote
0
down vote
favorite
I have used datasample()
and randsample()
to select specific no. of items randomly from a vector. But I have to specify the no. of items explicitly. Is there any way to specify percentage instead?
matlab
3
Why can't you don = floor(% * N)
, wheren
is the number of items you have to specify,%
is the percentage you want, andN
is the total number of elements in the vector? This seems pretty straightforward...
– Wolfie
Nov 10 at 12:19
@Wolfie thanks it worked, please post it as answer so that I may mark it correct
– Talha Yousuf
Nov 10 at 13:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have used datasample()
and randsample()
to select specific no. of items randomly from a vector. But I have to specify the no. of items explicitly. Is there any way to specify percentage instead?
matlab
I have used datasample()
and randsample()
to select specific no. of items randomly from a vector. But I have to specify the no. of items explicitly. Is there any way to specify percentage instead?
matlab
matlab
asked Nov 10 at 12:06
Talha Yousuf
203
203
3
Why can't you don = floor(% * N)
, wheren
is the number of items you have to specify,%
is the percentage you want, andN
is the total number of elements in the vector? This seems pretty straightforward...
– Wolfie
Nov 10 at 12:19
@Wolfie thanks it worked, please post it as answer so that I may mark it correct
– Talha Yousuf
Nov 10 at 13:32
add a comment |
3
Why can't you don = floor(% * N)
, wheren
is the number of items you have to specify,%
is the percentage you want, andN
is the total number of elements in the vector? This seems pretty straightforward...
– Wolfie
Nov 10 at 12:19
@Wolfie thanks it worked, please post it as answer so that I may mark it correct
– Talha Yousuf
Nov 10 at 13:32
3
3
Why can't you do
n = floor(% * N)
, where n
is the number of items you have to specify, %
is the percentage you want, and N
is the total number of elements in the vector? This seems pretty straightforward...– Wolfie
Nov 10 at 12:19
Why can't you do
n = floor(% * N)
, where n
is the number of items you have to specify, %
is the percentage you want, and N
is the total number of elements in the vector? This seems pretty straightforward...– Wolfie
Nov 10 at 12:19
@Wolfie thanks it worked, please post it as answer so that I may mark it correct
– Talha Yousuf
Nov 10 at 13:32
@Wolfie thanks it worked, please post it as answer so that I may mark it correct
– Talha Yousuf
Nov 10 at 13:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
You can very easily convert between the percentage of data you want, and the number which is required by the functions you want to use...
n = floor( pct * N );
Where n
is the number of items you have to specify, pct
is the percentage you want to input (between 0 and 1), and N
is the total number of elements in the vector.
You could use a different rounding function to floor
(ceil
/round
), but floor
is a good go-to so that you protect against requesting n > N
elements due to numerical error or otherwise.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can very easily convert between the percentage of data you want, and the number which is required by the functions you want to use...
n = floor( pct * N );
Where n
is the number of items you have to specify, pct
is the percentage you want to input (between 0 and 1), and N
is the total number of elements in the vector.
You could use a different rounding function to floor
(ceil
/round
), but floor
is a good go-to so that you protect against requesting n > N
elements due to numerical error or otherwise.
add a comment |
up vote
2
down vote
You can very easily convert between the percentage of data you want, and the number which is required by the functions you want to use...
n = floor( pct * N );
Where n
is the number of items you have to specify, pct
is the percentage you want to input (between 0 and 1), and N
is the total number of elements in the vector.
You could use a different rounding function to floor
(ceil
/round
), but floor
is a good go-to so that you protect against requesting n > N
elements due to numerical error or otherwise.
add a comment |
up vote
2
down vote
up vote
2
down vote
You can very easily convert between the percentage of data you want, and the number which is required by the functions you want to use...
n = floor( pct * N );
Where n
is the number of items you have to specify, pct
is the percentage you want to input (between 0 and 1), and N
is the total number of elements in the vector.
You could use a different rounding function to floor
(ceil
/round
), but floor
is a good go-to so that you protect against requesting n > N
elements due to numerical error or otherwise.
You can very easily convert between the percentage of data you want, and the number which is required by the functions you want to use...
n = floor( pct * N );
Where n
is the number of items you have to specify, pct
is the percentage you want to input (between 0 and 1), and N
is the total number of elements in the vector.
You could use a different rounding function to floor
(ceil
/round
), but floor
is a good go-to so that you protect against requesting n > N
elements due to numerical error or otherwise.
answered Nov 10 at 13:37
Wolfie
14.4k51741
14.4k51741
add a comment |
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238772%2fhow-to-select-specific-percentage-of-items-from-a-vector-in-matlab%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
3
Why can't you do
n = floor(% * N)
, wheren
is the number of items you have to specify,%
is the percentage you want, andN
is the total number of elements in the vector? This seems pretty straightforward...– Wolfie
Nov 10 at 12:19
@Wolfie thanks it worked, please post it as answer so that I may mark it correct
– Talha Yousuf
Nov 10 at 13:32