Count values in a vector less than each one of the elements in another vector
I have two vectors r
and d
and I'd like to know the number of times r<d(i)
where i=1:length(d)
.
r=rand(1,1E7);
d=linspace(0,1,10);
So far I've got the following, but it's not very elegant:
for i=1:length(d)
sum(r<d(i))
end
This is an example in R but I'm not really sure this would work for matlab:
Finding number of elements in one vector that are less than an element in another vector
arrays matlab vector
add a comment |
I have two vectors r
and d
and I'd like to know the number of times r<d(i)
where i=1:length(d)
.
r=rand(1,1E7);
d=linspace(0,1,10);
So far I've got the following, but it's not very elegant:
for i=1:length(d)
sum(r<d(i))
end
This is an example in R but I'm not really sure this would work for matlab:
Finding number of elements in one vector that are less than an element in another vector
arrays matlab vector
add a comment |
I have two vectors r
and d
and I'd like to know the number of times r<d(i)
where i=1:length(d)
.
r=rand(1,1E7);
d=linspace(0,1,10);
So far I've got the following, but it's not very elegant:
for i=1:length(d)
sum(r<d(i))
end
This is an example in R but I'm not really sure this would work for matlab:
Finding number of elements in one vector that are less than an element in another vector
arrays matlab vector
I have two vectors r
and d
and I'd like to know the number of times r<d(i)
where i=1:length(d)
.
r=rand(1,1E7);
d=linspace(0,1,10);
So far I've got the following, but it's not very elegant:
for i=1:length(d)
sum(r<d(i))
end
This is an example in R but I'm not really sure this would work for matlab:
Finding number of elements in one vector that are less than an element in another vector
arrays matlab vector
arrays matlab vector
edited Nov 16 '18 at 16:48
HCAI
asked Nov 16 '18 at 16:26
HCAIHCAI
57541338
57541338
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use singleton expansion with bsxfun
: faster, more elegant than the loop, but also more memory-intensive:
result = sum(bsxfun(@lt, r(:), d(:).'), 1);
In recent Matlab versions bsxfun
can be dropped thanks to implicit singleton expansion:
result = sum(r(:)<d(:).', 1);
An alternative approach is to use the histcounts
function with the 'cumcount'
option:
result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
result = result(1:end-1);
1
aw, 50 seconds late...
– Brice
Nov 16 '18 at 16:36
@Brice ¯_(ツ)_/¯ :-D
– Luis Mendo
Nov 16 '18 at 16:37
Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.
– HCAI
Nov 16 '18 at 16:46
@HCAI Thehistcounts
approach should be better then
– Luis Mendo
Nov 16 '18 at 16:47
1
Thank you very much, I'd like to accept your answer. The histcount is super fast!!!
– HCAI
Nov 16 '18 at 16:57
add a comment |
You may build a matrix flagging values from vector r
inferior to values from vector d
in one time with bsxfun
, then sum the values:
flag=bsxfun(@lt,r',d);
result=sum(flag,1);
Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.
– HCAI
Nov 16 '18 at 16:58
add a comment |
For each element in d
, count how many times this element is bigger than the elements in r
, which is equivalent to your problem.
r=rand(1,10);
d=linspace(0,1,10);
result = sum(d>r(:))
Output:
result =
0 0 1 2 7 8 8 8 9 10
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%2f53341823%2fcount-values-in-a-vector-less-than-each-one-of-the-elements-in-another-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use singleton expansion with bsxfun
: faster, more elegant than the loop, but also more memory-intensive:
result = sum(bsxfun(@lt, r(:), d(:).'), 1);
In recent Matlab versions bsxfun
can be dropped thanks to implicit singleton expansion:
result = sum(r(:)<d(:).', 1);
An alternative approach is to use the histcounts
function with the 'cumcount'
option:
result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
result = result(1:end-1);
1
aw, 50 seconds late...
– Brice
Nov 16 '18 at 16:36
@Brice ¯_(ツ)_/¯ :-D
– Luis Mendo
Nov 16 '18 at 16:37
Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.
– HCAI
Nov 16 '18 at 16:46
@HCAI Thehistcounts
approach should be better then
– Luis Mendo
Nov 16 '18 at 16:47
1
Thank you very much, I'd like to accept your answer. The histcount is super fast!!!
– HCAI
Nov 16 '18 at 16:57
add a comment |
You can use singleton expansion with bsxfun
: faster, more elegant than the loop, but also more memory-intensive:
result = sum(bsxfun(@lt, r(:), d(:).'), 1);
In recent Matlab versions bsxfun
can be dropped thanks to implicit singleton expansion:
result = sum(r(:)<d(:).', 1);
An alternative approach is to use the histcounts
function with the 'cumcount'
option:
result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
result = result(1:end-1);
1
aw, 50 seconds late...
– Brice
Nov 16 '18 at 16:36
@Brice ¯_(ツ)_/¯ :-D
– Luis Mendo
Nov 16 '18 at 16:37
Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.
– HCAI
Nov 16 '18 at 16:46
@HCAI Thehistcounts
approach should be better then
– Luis Mendo
Nov 16 '18 at 16:47
1
Thank you very much, I'd like to accept your answer. The histcount is super fast!!!
– HCAI
Nov 16 '18 at 16:57
add a comment |
You can use singleton expansion with bsxfun
: faster, more elegant than the loop, but also more memory-intensive:
result = sum(bsxfun(@lt, r(:), d(:).'), 1);
In recent Matlab versions bsxfun
can be dropped thanks to implicit singleton expansion:
result = sum(r(:)<d(:).', 1);
An alternative approach is to use the histcounts
function with the 'cumcount'
option:
result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
result = result(1:end-1);
You can use singleton expansion with bsxfun
: faster, more elegant than the loop, but also more memory-intensive:
result = sum(bsxfun(@lt, r(:), d(:).'), 1);
In recent Matlab versions bsxfun
can be dropped thanks to implicit singleton expansion:
result = sum(r(:)<d(:).', 1);
An alternative approach is to use the histcounts
function with the 'cumcount'
option:
result = histcounts(r(:), [-inf; d(:); inf], 'Normalization', 'cumcount');
result = result(1:end-1);
edited Nov 16 '18 at 16:48
answered Nov 16 '18 at 16:34
Luis MendoLuis Mendo
93.2k1154122
93.2k1154122
1
aw, 50 seconds late...
– Brice
Nov 16 '18 at 16:36
@Brice ¯_(ツ)_/¯ :-D
– Luis Mendo
Nov 16 '18 at 16:37
Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.
– HCAI
Nov 16 '18 at 16:46
@HCAI Thehistcounts
approach should be better then
– Luis Mendo
Nov 16 '18 at 16:47
1
Thank you very much, I'd like to accept your answer. The histcount is super fast!!!
– HCAI
Nov 16 '18 at 16:57
add a comment |
1
aw, 50 seconds late...
– Brice
Nov 16 '18 at 16:36
@Brice ¯_(ツ)_/¯ :-D
– Luis Mendo
Nov 16 '18 at 16:37
Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.
– HCAI
Nov 16 '18 at 16:46
@HCAI Thehistcounts
approach should be better then
– Luis Mendo
Nov 16 '18 at 16:47
1
Thank you very much, I'd like to accept your answer. The histcount is super fast!!!
– HCAI
Nov 16 '18 at 16:57
1
1
aw, 50 seconds late...
– Brice
Nov 16 '18 at 16:36
aw, 50 seconds late...
– Brice
Nov 16 '18 at 16:36
@Brice ¯_(ツ)_/¯ :-D
– Luis Mendo
Nov 16 '18 at 16:37
@Brice ¯_(ツ)_/¯ :-D
– Luis Mendo
Nov 16 '18 at 16:37
Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.
– HCAI
Nov 16 '18 at 16:46
Thank you for such a quick reply! The first one works for me with the example vectors but my actual r is 1E7 in length.... whereas d is only 10. It says i will run out of memory. Shall I update my question to reflect this. I'm sorry for the confusion.
– HCAI
Nov 16 '18 at 16:46
@HCAI The
histcounts
approach should be better then– Luis Mendo
Nov 16 '18 at 16:47
@HCAI The
histcounts
approach should be better then– Luis Mendo
Nov 16 '18 at 16:47
1
1
Thank you very much, I'd like to accept your answer. The histcount is super fast!!!
– HCAI
Nov 16 '18 at 16:57
Thank you very much, I'd like to accept your answer. The histcount is super fast!!!
– HCAI
Nov 16 '18 at 16:57
add a comment |
You may build a matrix flagging values from vector r
inferior to values from vector d
in one time with bsxfun
, then sum the values:
flag=bsxfun(@lt,r',d);
result=sum(flag,1);
Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.
– HCAI
Nov 16 '18 at 16:58
add a comment |
You may build a matrix flagging values from vector r
inferior to values from vector d
in one time with bsxfun
, then sum the values:
flag=bsxfun(@lt,r',d);
result=sum(flag,1);
Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.
– HCAI
Nov 16 '18 at 16:58
add a comment |
You may build a matrix flagging values from vector r
inferior to values from vector d
in one time with bsxfun
, then sum the values:
flag=bsxfun(@lt,r',d);
result=sum(flag,1);
You may build a matrix flagging values from vector r
inferior to values from vector d
in one time with bsxfun
, then sum the values:
flag=bsxfun(@lt,r',d);
result=sum(flag,1);
answered Nov 16 '18 at 16:35
BriceBrice
1,400110
1,400110
Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.
– HCAI
Nov 16 '18 at 16:58
add a comment |
Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.
– HCAI
Nov 16 '18 at 16:58
Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.
– HCAI
Nov 16 '18 at 16:58
Thank you for your answer, I'd like to accept Luis' as the histcount seems to be quite a bit quicker although this also works.
– HCAI
Nov 16 '18 at 16:58
add a comment |
For each element in d
, count how many times this element is bigger than the elements in r
, which is equivalent to your problem.
r=rand(1,10);
d=linspace(0,1,10);
result = sum(d>r(:))
Output:
result =
0 0 1 2 7 8 8 8 9 10
add a comment |
For each element in d
, count how many times this element is bigger than the elements in r
, which is equivalent to your problem.
r=rand(1,10);
d=linspace(0,1,10);
result = sum(d>r(:))
Output:
result =
0 0 1 2 7 8 8 8 9 10
add a comment |
For each element in d
, count how many times this element is bigger than the elements in r
, which is equivalent to your problem.
r=rand(1,10);
d=linspace(0,1,10);
result = sum(d>r(:))
Output:
result =
0 0 1 2 7 8 8 8 9 10
For each element in d
, count how many times this element is bigger than the elements in r
, which is equivalent to your problem.
r=rand(1,10);
d=linspace(0,1,10);
result = sum(d>r(:))
Output:
result =
0 0 1 2 7 8 8 8 9 10
edited Nov 16 '18 at 16:54
answered Nov 16 '18 at 16:39
Banghua ZhaoBanghua Zhao
1,2771719
1,2771719
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%2f53341823%2fcount-values-in-a-vector-less-than-each-one-of-the-elements-in-another-vector%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