フィルターのクリア

Excluded Digits from vector

1 回表示 (過去 30 日間)
Harish Maradana
Harish Maradana 2014 年 8 月 22 日
コメント済み: Guillaume 2014 年 8 月 22 日
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
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.

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2014 年 8 月 22 日
I expect there is a much cleaner method, but here is one that works:
vector = [1 2 5 13 55 23 15];
exDigit = 5;
v = vector;
hasDigit = false(1,numel(v));
while max(v>=1)
hasDigit = hasDigit | mod(v,10)==exDigit;
v = floor(v/10);
end
new_vector = vector(not(hasDigit))

その他の回答 (2 件)

Guillaume
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 件のコメント
the cyclist
the cyclist 2014 年 8 月 22 日
I stole this solution and posted it to Cody. As I write this, it is the leader.
Guillaume
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
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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by