how to find non-numeric value from a matrix?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
I'm trying to give a numeric value to a non-numeric values. It seems to be a very simple problem but I just couldn't figure it out. Let's say I want to find the store number according to my t-shirt size.
First I define different sizes.
size = {'large', 'medium', 'small'};
Then, convert this non-numeric value to a numeric value for further use.
tshirt = find (size == 'medium');
This command doesn't work...I was hoping this command will give me back '2' which would be the column numer for 'medium' to give instructions to go store #2.
Is there any way to make this work? I'd appreciate a lot for any help/advices!
댓글 수: 0
채택된 답변
Jonathan Epperl
2012년 10월 17일
To compare strings, you'll need strcmp or strcmpi, depending on whether you want case sensitive or insensitive.
>> tshirts = {'sm','med','lar'};
>> nr = find(strcmpi(tshirts,'med'))
nr =
2
댓글 수: 0
추가 답변 (1개)
Sean de Wolski
2012년 10월 17일
First and most importantly: don't name a variable size!!!! This is a super useful builtin function that when overwritten can cause disastrous effects.
Second, use strcmp
tsize = {'large', 'medium', 'small'};
find(strcmp(tsize,'medium'))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!