How to get binary output for unique()?
[uniq_elements,uniq_indices]=unique(x)
shows unique element in e.g. vector x
.
What would be a simple (i.e. one-liner) and efficient(i.e. fast execution time) way to simply and only indicate 'is this value unique or not' and 'if not' output the first of the series of duplicates? Is there maybe a way to solve this using logical operators? It could be a start to simply have a function outputting 'is this value unique or not'.
example code:
x=[5 5 2 2 3 4 4 1 1 1]; %example values
[~,uniq_indices]=unique(x); %returns [1,3,5,6,8]
result=false(size(x));
result(uniq_indices)=1
%console ouput:
result =
1×10 logical array
1 0 1 0 1 1 0 1 0 0
Basically my presumption is that - because only logical output is needed - the above code could be achieved faster/easier.
matlab
|
show 7 more comments
[uniq_elements,uniq_indices]=unique(x)
shows unique element in e.g. vector x
.
What would be a simple (i.e. one-liner) and efficient(i.e. fast execution time) way to simply and only indicate 'is this value unique or not' and 'if not' output the first of the series of duplicates? Is there maybe a way to solve this using logical operators? It could be a start to simply have a function outputting 'is this value unique or not'.
example code:
x=[5 5 2 2 3 4 4 1 1 1]; %example values
[~,uniq_indices]=unique(x); %returns [1,3,5,6,8]
result=false(size(x));
result(uniq_indices)=1
%console ouput:
result =
1×10 logical array
1 0 1 0 1 1 0 1 0 0
Basically my presumption is that - because only logical output is needed - the above code could be achieved faster/easier.
matlab
1
Your code doesn’t mark unique items, it marks the first of each set of duplicates too. I interpret “is unique” differently.
– Cris Luengo
Dec 31 '18 at 16:51
1
Why is your existing code not satisfactory? It's pretty rapid in my quick tests...
– Wolfie
Dec 31 '18 at 17:02
@Wolfie: it might be, I was just trying to bounce it off because I had a gutfeeling it could be done better - as stated, it's a personal presumption and could be wrong (in fact, it would be my preference)
– user2305193
Dec 31 '18 at 17:05
1
What I'm aiming at is this;unique
uses asort
internally (you can see this by runningedit unique
), and then gets all the positions where neighbouring elements aren't equal. To get theuniq_indices
, the sort is inverted, and an extrafind
is called (as well as all the internal checks regardless). If you are happy to have a sorted input array, and the corresponding logical array of unique values, you can just do thesort
anddiff
yourself
– Wolfie
Dec 31 '18 at 17:16
1
MATLAB’sunique
creates an array where all elements are unique. It retrieves one example of each group of identical elements. It does not claim to find the elements that are unique. If you’re looking for either of those options, I don’t know what to aim for. I suggest you decide which one you need. :)
– Cris Luengo
Dec 31 '18 at 17:34
|
show 7 more comments
[uniq_elements,uniq_indices]=unique(x)
shows unique element in e.g. vector x
.
What would be a simple (i.e. one-liner) and efficient(i.e. fast execution time) way to simply and only indicate 'is this value unique or not' and 'if not' output the first of the series of duplicates? Is there maybe a way to solve this using logical operators? It could be a start to simply have a function outputting 'is this value unique or not'.
example code:
x=[5 5 2 2 3 4 4 1 1 1]; %example values
[~,uniq_indices]=unique(x); %returns [1,3,5,6,8]
result=false(size(x));
result(uniq_indices)=1
%console ouput:
result =
1×10 logical array
1 0 1 0 1 1 0 1 0 0
Basically my presumption is that - because only logical output is needed - the above code could be achieved faster/easier.
matlab
[uniq_elements,uniq_indices]=unique(x)
shows unique element in e.g. vector x
.
What would be a simple (i.e. one-liner) and efficient(i.e. fast execution time) way to simply and only indicate 'is this value unique or not' and 'if not' output the first of the series of duplicates? Is there maybe a way to solve this using logical operators? It could be a start to simply have a function outputting 'is this value unique or not'.
example code:
x=[5 5 2 2 3 4 4 1 1 1]; %example values
[~,uniq_indices]=unique(x); %returns [1,3,5,6,8]
result=false(size(x));
result(uniq_indices)=1
%console ouput:
result =
1×10 logical array
1 0 1 0 1 1 0 1 0 0
Basically my presumption is that - because only logical output is needed - the above code could be achieved faster/easier.
matlab
matlab
edited Dec 31 '18 at 17:16
user2305193
asked Dec 31 '18 at 16:45
user2305193user2305193
857621
857621
1
Your code doesn’t mark unique items, it marks the first of each set of duplicates too. I interpret “is unique” differently.
– Cris Luengo
Dec 31 '18 at 16:51
1
Why is your existing code not satisfactory? It's pretty rapid in my quick tests...
– Wolfie
Dec 31 '18 at 17:02
@Wolfie: it might be, I was just trying to bounce it off because I had a gutfeeling it could be done better - as stated, it's a personal presumption and could be wrong (in fact, it would be my preference)
– user2305193
Dec 31 '18 at 17:05
1
What I'm aiming at is this;unique
uses asort
internally (you can see this by runningedit unique
), and then gets all the positions where neighbouring elements aren't equal. To get theuniq_indices
, the sort is inverted, and an extrafind
is called (as well as all the internal checks regardless). If you are happy to have a sorted input array, and the corresponding logical array of unique values, you can just do thesort
anddiff
yourself
– Wolfie
Dec 31 '18 at 17:16
1
MATLAB’sunique
creates an array where all elements are unique. It retrieves one example of each group of identical elements. It does not claim to find the elements that are unique. If you’re looking for either of those options, I don’t know what to aim for. I suggest you decide which one you need. :)
– Cris Luengo
Dec 31 '18 at 17:34
|
show 7 more comments
1
Your code doesn’t mark unique items, it marks the first of each set of duplicates too. I interpret “is unique” differently.
– Cris Luengo
Dec 31 '18 at 16:51
1
Why is your existing code not satisfactory? It's pretty rapid in my quick tests...
– Wolfie
Dec 31 '18 at 17:02
@Wolfie: it might be, I was just trying to bounce it off because I had a gutfeeling it could be done better - as stated, it's a personal presumption and could be wrong (in fact, it would be my preference)
– user2305193
Dec 31 '18 at 17:05
1
What I'm aiming at is this;unique
uses asort
internally (you can see this by runningedit unique
), and then gets all the positions where neighbouring elements aren't equal. To get theuniq_indices
, the sort is inverted, and an extrafind
is called (as well as all the internal checks regardless). If you are happy to have a sorted input array, and the corresponding logical array of unique values, you can just do thesort
anddiff
yourself
– Wolfie
Dec 31 '18 at 17:16
1
MATLAB’sunique
creates an array where all elements are unique. It retrieves one example of each group of identical elements. It does not claim to find the elements that are unique. If you’re looking for either of those options, I don’t know what to aim for. I suggest you decide which one you need. :)
– Cris Luengo
Dec 31 '18 at 17:34
1
1
Your code doesn’t mark unique items, it marks the first of each set of duplicates too. I interpret “is unique” differently.
– Cris Luengo
Dec 31 '18 at 16:51
Your code doesn’t mark unique items, it marks the first of each set of duplicates too. I interpret “is unique” differently.
– Cris Luengo
Dec 31 '18 at 16:51
1
1
Why is your existing code not satisfactory? It's pretty rapid in my quick tests...
– Wolfie
Dec 31 '18 at 17:02
Why is your existing code not satisfactory? It's pretty rapid in my quick tests...
– Wolfie
Dec 31 '18 at 17:02
@Wolfie: it might be, I was just trying to bounce it off because I had a gutfeeling it could be done better - as stated, it's a personal presumption and could be wrong (in fact, it would be my preference)
– user2305193
Dec 31 '18 at 17:05
@Wolfie: it might be, I was just trying to bounce it off because I had a gutfeeling it could be done better - as stated, it's a personal presumption and could be wrong (in fact, it would be my preference)
– user2305193
Dec 31 '18 at 17:05
1
1
What I'm aiming at is this;
unique
uses a sort
internally (you can see this by running edit unique
), and then gets all the positions where neighbouring elements aren't equal. To get the uniq_indices
, the sort is inverted, and an extra find
is called (as well as all the internal checks regardless). If you are happy to have a sorted input array, and the corresponding logical array of unique values, you can just do the sort
and diff
yourself– Wolfie
Dec 31 '18 at 17:16
What I'm aiming at is this;
unique
uses a sort
internally (you can see this by running edit unique
), and then gets all the positions where neighbouring elements aren't equal. To get the uniq_indices
, the sort is inverted, and an extra find
is called (as well as all the internal checks regardless). If you are happy to have a sorted input array, and the corresponding logical array of unique values, you can just do the sort
and diff
yourself– Wolfie
Dec 31 '18 at 17:16
1
1
MATLAB’s
unique
creates an array where all elements are unique. It retrieves one example of each group of identical elements. It does not claim to find the elements that are unique. If you’re looking for either of those options, I don’t know what to aim for. I suggest you decide which one you need. :)– Cris Luengo
Dec 31 '18 at 17:34
MATLAB’s
unique
creates an array where all elements are unique. It retrieves one example of each group of identical elements. It does not claim to find the elements that are unique. If you’re looking for either of those options, I don’t know what to aim for. I suggest you decide which one you need. :)– Cris Luengo
Dec 31 '18 at 17:34
|
show 7 more comments
1 Answer
1
active
oldest
votes
Approach 1
This is memory-intensive, as it creates an intermediate n
×n
logical matrix, where n
is the size of x
:
result = sum(triu(bsxfun(@eq, x, x.')))==1;
Since R2016b, this can be expressed more succintly as
result = sum(triu(x==x.'))==1;
How it works
bsxfun(@eq, x, x.')
(or x==x.'
) creates a square matrix of equality comparisons.
triu
keeps only the upper triangular part, so that each element is only compared with previous ones or with itself.
sum
gives the sum of each column. If the sum is 1
this means that the element is not equal to any previous elements, only to itself.
Approach 2
This is slightly more memory-efficient. It creates an intermediate m
×n
logical matrix, where m
is the number of unique elements and n
is the total number of elements:
xu = unique(x);
result = any(diff([false(numel(xu),1) bsxfun(@eq, x, xu.')],,2)==1);
Since R2016b, the second line can be replaced by
result = any(diff([false(numel(xu),1) x==xu.'],,2)==1);
How it works
This creates a matrix, say A
, of comparisons of each unique element of x
(row index of A
) with each element of x
(column index of A
). Let B
denote the result of appending a column of false
to A
.
An element that appears in x
for the first time corresponds to a [0 1]
subsequence in the corresponding row of B
. To detect this, diff
is applied along each row, and the result is compared with 1
(which is the increment between the elements in [0 1]
).
1
it may be more memory intensive but it also runs about 1.5-1.75x faster than my code using tic toc on my machine. And it's codegasmic! Can you explain the logic of usingtriu()
?
– user2305193
Dec 31 '18 at 17:39
1
@user2305193 I added an explanation, and another method
– Luis Mendo
Dec 31 '18 at 17:59
@CrisLuengo Thanks! It's actually not needed, and removing it increases speed
– Luis Mendo
Dec 31 '18 at 18:03
I tested the first approach for larger vectors, it appears to become more efficient the larger the vector
– user2305193
Dec 31 '18 at 18:03
1
@user2305193 It should be fast as long as there is enough memory. But for very large vectors memory will be the limiting factor
– Luis Mendo
Dec 31 '18 at 18:04
|
show 3 more comments
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%2f53989627%2fhow-to-get-binary-output-for-unique%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
Approach 1
This is memory-intensive, as it creates an intermediate n
×n
logical matrix, where n
is the size of x
:
result = sum(triu(bsxfun(@eq, x, x.')))==1;
Since R2016b, this can be expressed more succintly as
result = sum(triu(x==x.'))==1;
How it works
bsxfun(@eq, x, x.')
(or x==x.'
) creates a square matrix of equality comparisons.
triu
keeps only the upper triangular part, so that each element is only compared with previous ones or with itself.
sum
gives the sum of each column. If the sum is 1
this means that the element is not equal to any previous elements, only to itself.
Approach 2
This is slightly more memory-efficient. It creates an intermediate m
×n
logical matrix, where m
is the number of unique elements and n
is the total number of elements:
xu = unique(x);
result = any(diff([false(numel(xu),1) bsxfun(@eq, x, xu.')],,2)==1);
Since R2016b, the second line can be replaced by
result = any(diff([false(numel(xu),1) x==xu.'],,2)==1);
How it works
This creates a matrix, say A
, of comparisons of each unique element of x
(row index of A
) with each element of x
(column index of A
). Let B
denote the result of appending a column of false
to A
.
An element that appears in x
for the first time corresponds to a [0 1]
subsequence in the corresponding row of B
. To detect this, diff
is applied along each row, and the result is compared with 1
(which is the increment between the elements in [0 1]
).
1
it may be more memory intensive but it also runs about 1.5-1.75x faster than my code using tic toc on my machine. And it's codegasmic! Can you explain the logic of usingtriu()
?
– user2305193
Dec 31 '18 at 17:39
1
@user2305193 I added an explanation, and another method
– Luis Mendo
Dec 31 '18 at 17:59
@CrisLuengo Thanks! It's actually not needed, and removing it increases speed
– Luis Mendo
Dec 31 '18 at 18:03
I tested the first approach for larger vectors, it appears to become more efficient the larger the vector
– user2305193
Dec 31 '18 at 18:03
1
@user2305193 It should be fast as long as there is enough memory. But for very large vectors memory will be the limiting factor
– Luis Mendo
Dec 31 '18 at 18:04
|
show 3 more comments
Approach 1
This is memory-intensive, as it creates an intermediate n
×n
logical matrix, where n
is the size of x
:
result = sum(triu(bsxfun(@eq, x, x.')))==1;
Since R2016b, this can be expressed more succintly as
result = sum(triu(x==x.'))==1;
How it works
bsxfun(@eq, x, x.')
(or x==x.'
) creates a square matrix of equality comparisons.
triu
keeps only the upper triangular part, so that each element is only compared with previous ones or with itself.
sum
gives the sum of each column. If the sum is 1
this means that the element is not equal to any previous elements, only to itself.
Approach 2
This is slightly more memory-efficient. It creates an intermediate m
×n
logical matrix, where m
is the number of unique elements and n
is the total number of elements:
xu = unique(x);
result = any(diff([false(numel(xu),1) bsxfun(@eq, x, xu.')],,2)==1);
Since R2016b, the second line can be replaced by
result = any(diff([false(numel(xu),1) x==xu.'],,2)==1);
How it works
This creates a matrix, say A
, of comparisons of each unique element of x
(row index of A
) with each element of x
(column index of A
). Let B
denote the result of appending a column of false
to A
.
An element that appears in x
for the first time corresponds to a [0 1]
subsequence in the corresponding row of B
. To detect this, diff
is applied along each row, and the result is compared with 1
(which is the increment between the elements in [0 1]
).
1
it may be more memory intensive but it also runs about 1.5-1.75x faster than my code using tic toc on my machine. And it's codegasmic! Can you explain the logic of usingtriu()
?
– user2305193
Dec 31 '18 at 17:39
1
@user2305193 I added an explanation, and another method
– Luis Mendo
Dec 31 '18 at 17:59
@CrisLuengo Thanks! It's actually not needed, and removing it increases speed
– Luis Mendo
Dec 31 '18 at 18:03
I tested the first approach for larger vectors, it appears to become more efficient the larger the vector
– user2305193
Dec 31 '18 at 18:03
1
@user2305193 It should be fast as long as there is enough memory. But for very large vectors memory will be the limiting factor
– Luis Mendo
Dec 31 '18 at 18:04
|
show 3 more comments
Approach 1
This is memory-intensive, as it creates an intermediate n
×n
logical matrix, where n
is the size of x
:
result = sum(triu(bsxfun(@eq, x, x.')))==1;
Since R2016b, this can be expressed more succintly as
result = sum(triu(x==x.'))==1;
How it works
bsxfun(@eq, x, x.')
(or x==x.'
) creates a square matrix of equality comparisons.
triu
keeps only the upper triangular part, so that each element is only compared with previous ones or with itself.
sum
gives the sum of each column. If the sum is 1
this means that the element is not equal to any previous elements, only to itself.
Approach 2
This is slightly more memory-efficient. It creates an intermediate m
×n
logical matrix, where m
is the number of unique elements and n
is the total number of elements:
xu = unique(x);
result = any(diff([false(numel(xu),1) bsxfun(@eq, x, xu.')],,2)==1);
Since R2016b, the second line can be replaced by
result = any(diff([false(numel(xu),1) x==xu.'],,2)==1);
How it works
This creates a matrix, say A
, of comparisons of each unique element of x
(row index of A
) with each element of x
(column index of A
). Let B
denote the result of appending a column of false
to A
.
An element that appears in x
for the first time corresponds to a [0 1]
subsequence in the corresponding row of B
. To detect this, diff
is applied along each row, and the result is compared with 1
(which is the increment between the elements in [0 1]
).
Approach 1
This is memory-intensive, as it creates an intermediate n
×n
logical matrix, where n
is the size of x
:
result = sum(triu(bsxfun(@eq, x, x.')))==1;
Since R2016b, this can be expressed more succintly as
result = sum(triu(x==x.'))==1;
How it works
bsxfun(@eq, x, x.')
(or x==x.'
) creates a square matrix of equality comparisons.
triu
keeps only the upper triangular part, so that each element is only compared with previous ones or with itself.
sum
gives the sum of each column. If the sum is 1
this means that the element is not equal to any previous elements, only to itself.
Approach 2
This is slightly more memory-efficient. It creates an intermediate m
×n
logical matrix, where m
is the number of unique elements and n
is the total number of elements:
xu = unique(x);
result = any(diff([false(numel(xu),1) bsxfun(@eq, x, xu.')],,2)==1);
Since R2016b, the second line can be replaced by
result = any(diff([false(numel(xu),1) x==xu.'],,2)==1);
How it works
This creates a matrix, say A
, of comparisons of each unique element of x
(row index of A
) with each element of x
(column index of A
). Let B
denote the result of appending a column of false
to A
.
An element that appears in x
for the first time corresponds to a [0 1]
subsequence in the corresponding row of B
. To detect this, diff
is applied along each row, and the result is compared with 1
(which is the increment between the elements in [0 1]
).
edited Dec 31 '18 at 18:05
answered Dec 31 '18 at 17:35
Luis MendoLuis Mendo
93.3k1154122
93.3k1154122
1
it may be more memory intensive but it also runs about 1.5-1.75x faster than my code using tic toc on my machine. And it's codegasmic! Can you explain the logic of usingtriu()
?
– user2305193
Dec 31 '18 at 17:39
1
@user2305193 I added an explanation, and another method
– Luis Mendo
Dec 31 '18 at 17:59
@CrisLuengo Thanks! It's actually not needed, and removing it increases speed
– Luis Mendo
Dec 31 '18 at 18:03
I tested the first approach for larger vectors, it appears to become more efficient the larger the vector
– user2305193
Dec 31 '18 at 18:03
1
@user2305193 It should be fast as long as there is enough memory. But for very large vectors memory will be the limiting factor
– Luis Mendo
Dec 31 '18 at 18:04
|
show 3 more comments
1
it may be more memory intensive but it also runs about 1.5-1.75x faster than my code using tic toc on my machine. And it's codegasmic! Can you explain the logic of usingtriu()
?
– user2305193
Dec 31 '18 at 17:39
1
@user2305193 I added an explanation, and another method
– Luis Mendo
Dec 31 '18 at 17:59
@CrisLuengo Thanks! It's actually not needed, and removing it increases speed
– Luis Mendo
Dec 31 '18 at 18:03
I tested the first approach for larger vectors, it appears to become more efficient the larger the vector
– user2305193
Dec 31 '18 at 18:03
1
@user2305193 It should be fast as long as there is enough memory. But for very large vectors memory will be the limiting factor
– Luis Mendo
Dec 31 '18 at 18:04
1
1
it may be more memory intensive but it also runs about 1.5-1.75x faster than my code using tic toc on my machine. And it's codegasmic! Can you explain the logic of using
triu()
?– user2305193
Dec 31 '18 at 17:39
it may be more memory intensive but it also runs about 1.5-1.75x faster than my code using tic toc on my machine. And it's codegasmic! Can you explain the logic of using
triu()
?– user2305193
Dec 31 '18 at 17:39
1
1
@user2305193 I added an explanation, and another method
– Luis Mendo
Dec 31 '18 at 17:59
@user2305193 I added an explanation, and another method
– Luis Mendo
Dec 31 '18 at 17:59
@CrisLuengo Thanks! It's actually not needed, and removing it increases speed
– Luis Mendo
Dec 31 '18 at 18:03
@CrisLuengo Thanks! It's actually not needed, and removing it increases speed
– Luis Mendo
Dec 31 '18 at 18:03
I tested the first approach for larger vectors, it appears to become more efficient the larger the vector
– user2305193
Dec 31 '18 at 18:03
I tested the first approach for larger vectors, it appears to become more efficient the larger the vector
– user2305193
Dec 31 '18 at 18:03
1
1
@user2305193 It should be fast as long as there is enough memory. But for very large vectors memory will be the limiting factor
– Luis Mendo
Dec 31 '18 at 18:04
@user2305193 It should be fast as long as there is enough memory. But for very large vectors memory will be the limiting factor
– Luis Mendo
Dec 31 '18 at 18:04
|
show 3 more comments
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%2f53989627%2fhow-to-get-binary-output-for-unique%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
1
Your code doesn’t mark unique items, it marks the first of each set of duplicates too. I interpret “is unique” differently.
– Cris Luengo
Dec 31 '18 at 16:51
1
Why is your existing code not satisfactory? It's pretty rapid in my quick tests...
– Wolfie
Dec 31 '18 at 17:02
@Wolfie: it might be, I was just trying to bounce it off because I had a gutfeeling it could be done better - as stated, it's a personal presumption and could be wrong (in fact, it would be my preference)
– user2305193
Dec 31 '18 at 17:05
1
What I'm aiming at is this;
unique
uses asort
internally (you can see this by runningedit unique
), and then gets all the positions where neighbouring elements aren't equal. To get theuniq_indices
, the sort is inverted, and an extrafind
is called (as well as all the internal checks regardless). If you are happy to have a sorted input array, and the corresponding logical array of unique values, you can just do thesort
anddiff
yourself– Wolfie
Dec 31 '18 at 17:16
1
MATLAB’s
unique
creates an array where all elements are unique. It retrieves one example of each group of identical elements. It does not claim to find the elements that are unique. If you’re looking for either of those options, I don’t know what to aim for. I suggest you decide which one you need. :)– Cris Luengo
Dec 31 '18 at 17:34