Excluded Digits from vector
이전 댓글 표시
vector=[1 2 5 13 55 23 15],excluded dig=5 then out=[1 2 13 23] ,another example vector=[3 24 7 9 18 55 67 71],excluded dig=7 then out=[3 24 9 18 55]
댓글 수: 1
the cyclist
2014년 8월 22일
편집: the cyclist
2014년 8월 22일
I posted this as a Cody problem . As you may know, Cody rewards brevity of code over all other things.
채택된 답변
추가 답변 (2개)
Guillaume
2014년 8월 22일
str2num(regexprep(num2str(vector), sprintf('\\<\\d*%d\\d*\\>', digit), ''))
Is a neat one liner but may not be faster than the cyclist answer due to the conversion to/from string and the use of regular expression.
댓글 수: 4
Pierre Benoit
2014년 8월 22일
편집: Pierre Benoit
2014년 8월 22일
Yeah, I used a similar method and found that his method was around 330 times faster.
Guillaume
2014년 8월 22일
Another potential method, faster than regexp but still slower than the cyclist's:
vector(arrayfun(@(n) all(num2str(n)-'0' ~= digit), vector))
the cyclist
2014년 8월 22일
I stole this solution and posted it to Cody. As I write this, it is the leader.
Guillaume
2014년 8월 22일
What? No fair! I should so rightly be the leader! ;)
Ha! Beaten with a variant on my regexp one-line :)
the cyclist
2014년 8월 22일
Here is a solution that came out of Cody. The inputs to the function are the vector v and the excluded digit d.
function ans = digitRemove(v,d)
I = [];
for i = 1 : length(v)
if ~any(ismember( num2str(v(i)) - '0' , d))
I = [I i];
end
end
v(I);
end
카테고리
도움말 센터 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!