how to know if a char vector contains unwanted characters?

17 ビュー (過去 30 日間)
Osama Alkurdi
Osama Alkurdi 2020 年 2 月 18 日
コメント済み: Osama Alkurdi 2020 年 2 月 18 日
I have this row vector
x=["0","1","2","3","4","5","6","7","8","9","."];
and let us say that there is a vector of char class called y
I want matlab to return true if all the characters in y is from x
and return false if there is any character in y out of x
  6 件のコメント
Osama Alkurdi
Osama Alkurdi 2020 年 2 月 18 日
i mean y
Osama Alkurdi
Osama Alkurdi 2020 年 2 月 18 日
sorry

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

回答 (3 件)

Adam Danz
Adam Danz 2020 年 2 月 18 日
編集済み: Adam Danz 2020 年 2 月 18 日
Following the description from your question, here's an anonymous function allCharsGood() with two inputs,
  • c is a 1xn char array
  • goodList is a 1xn string array
allCharsGood converts the char array c to a string array and then determines whether all elements of c are listed in goodList.
It returns true if all elements of c are in goodList.
It returns false if at least one element of c is not in goodList.
x=["0","1","2","3","4","5","6","7","8","9","."]; % String array
y1 = '3.1415926'; % char array
y2 = '9.56e20'; % char array
allCharsGood = @(c, goodList)all(ismember(string(cellstr(c')'), goodList));
allMatch = allCharsGood(y1,x) % true
allMatch = allCharsGood(y2,x) % false

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2020 年 2 月 18 日
y=["5","6","10"];%y example
condition=all(ismember(y,x))

Joseph Cheng
Joseph Cheng 2020 年 2 月 18 日
check out the function ismember here is snippet of test code
x=["0","1","2","3","4","5","6","7","8","9","."];
y = ["3","3","5","8","1","2"];
checks = ismember(y,x);
disp(x)
disp(y)
disp(checks)
if sum(checks)==numel(y)
ret = true
else
ret = false
end
y = ["10","44","5","8","1","2"];
checks = ismember(y,x);
if sum(checks)==numel(y)
ret = true
else
ret = false
end
here you can see the two different y character arrays are checked against each other one by one.
  1 件のコメント
Stephen23
Stephen23 2020 年 2 月 18 日
編集済み: Stephen23 2020 年 2 月 18 日
Complex:
if sum(checks)==numel(y)
ret = true
else
ret = false
end
Simple and efficient:
ret = all(checks)

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

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by