matlab cody学习笔记 day10

(1)Check if number exists in vector
Return 1 if number a exists in vector b otherwise return 0.
a = 3;
b = [1,2,4];
Returns 0.
a = 3;
b = [1,2,3];
Returns 1.
这道题目还是很简单的,第一眼就觉得用find函数,但是还需要加判断。
答:
if true(find(b == a))
y = 1
else
y = 0
end
答:
y = ~isempty(b(b == a))
或者更简单一点:
答:
y = ismember(a,b)

(2)Remove the vowels
Remove all the vowels in the given phrase.
Example:
Input s1 = ‘Jack and Jill went up the hill’
Output s2 is ‘Jck nd Jll wnt p th hll’
因为不经常用元音,所以这个道题目一看有点生疏,其实和数字差不多,就是一个选择判断:
答:
s2 = [];
s3=‘aAeEiIoOuU’;
L=strlength(s1);
L1=strlength(s3);
flag=0;
j=1;
for i=1:L
for k=1:L1
if s1(i)s3(k)
flag=1;
continue;
end
end
if flag
0
s2(j)=s1(i);
j=j+1;
end
flag=0;
end
end
注意strlength和length的区别。
其他刷到的题目就很简单了。

猜你喜欢

转载自blog.csdn.net/yxnooo1/article/details/113898413