Remove elements from string array

21 次查看(过去 30 天)
anon
anon 2019-11-26
回答: Guillaume 2019-11-26
What is the simpest way to remove string elements from an array? e.g. arr = [1, 2, 3, "x", "y", 10] would turn into [1,2,3,10]
  4 个评论
Guillaume
Guillaume 2019-11-26
What is the rule that dictates which elements should be removed from the string array?
anon
anon 2019-11-26
Create a new array that contains only integers. So after removing all non-integers the array would need to be turned into a numeric array e.g. [1,2,3,10]

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2019-11-26
One possible way:
numericarray = double(yourstringarray); %convert string array to numeric. Text that can't be converted to numeric will end up as NaN.
numericarray = numericarray(mod(numericarray, 1) == 0); %only keep numbers that are integers. Will also remove NaNs.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by