Im trying to run a code to create a subset of a group i.e females who are credit worthy but im struggling with getting program to recognize f for females
>> worthyf = credit(credit.gender=='f' & credit.Risk_class==1,:);
Undefined operator '==' for input arguments of type 'cell'.

 採用された回答

Jesus Sanchez
Jesus Sanchez 2019 年 2 月 19 日

0 投票

== only works for numbers. You are comparing strings and therefore you need to use "strcmp"
An example in your code:
worthyf = credit(strcmp(credit.gender,'f') & credit.Risk_class==1,:);

5 件のコメント

MZINGAYE MUBAYA
MZINGAYE MUBAYA 2019 年 2 月 19 日
Perfect Thank you so much for prompt response
Guillaume
Guillaume 2019 年 2 月 20 日
== only works for numbers.
Not quite. It also works for single characters, and for categorical arrays, and for any class that overloads the == operator. Not that if x is a (scalar) categorical array.
if x == 'something'
disp('it''s something');
elseif x == 'somethingelse'
disp('it''s something else');
end
is perfectly valid. Try it with
x = categorical({'somethingelse'});
Jesus Sanchez
Jesus Sanchez 2019 年 2 月 20 日
I did not know that. Thank you for the clarification and information :)
Guillaume
Guillaume 2019 年 2 月 20 日
Actually, I completely forgot the most relevant class here, the newish string class. This has an overloaded == operator that is equivalent to strcmp. Unfortunately, that's going to lead to lot of confusion for beginner because indeed == doesn't work as they expect for char arrays (which unfortunately many people call strings) but does work for actual strings.
s = "somethingelse"; %an actual matlab string
if s == "something"
disp('it''s something');
elseif s == "somethingelse"
disp('it''something else');
end
works as expected
s = 'something else'
if s == 'something' %proper syntax: if strcmp(s, 'something')
disp('it''s something');
elseif s == 'somethingelse'
disp('it''something else');
end
does not work. you have to use strcmp. strcmp also works for actual strings, so it may be safer to always use that.
MZINGAYE MUBAYA
MZINGAYE MUBAYA 2019 年 2 月 21 日
So basically use strcmp for my data set

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Environment and Settings についてさらに検索

製品

リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by