检验矩阵中是否有某个元素
ismember(A,a)
find函数
>> a=[1 2 3]
flag = find(a==2);
if flag
disp('here')
else
disp('not here')
end
a =
1 2 3
here
>>
有一组数据,数据中的‘甲’,‘乙’,‘丙’是不同的类型,对其求和并按照不同的全脂计算总量
>> a = ['甲' ;'乙'; '乙'; '甲';'丙' ;'甲'; '丙']
result = sum(ismember(a,'甲'))*2 + sum(ismember(a,'乙'))*3+sum(ismember(a, '丙'))*4
a =
7×1 char 数组
'甲'
'乙'
'乙'
'甲'
'丙'
'甲'
'丙'
result =
20
>>
一组数列中,不重复的元素的个数
%定义数列
h1 = [ 41 19 43 271 263 51 20 49 337 265 329 270 21 329 45 18 264 42 329 18]
%统计h1的信息,值,个数,比例
[hh1] = tabulate(h1);
%不重复的个数
number1 = size(unique(hh1(:,1)))-size(find(hh1(:,2)==0))
%ans
h1 =
41 19 43 271 263 51 20 49 337 265 329 270 21 329 45 18 264 42 329 18
number1 =
17 0
统计 两个矩阵中的 相同的元素的个数
% p3 type_c 是两个列向量
[ia,ib,ic] = intersect(p3',type_c')
type_a_number_1 = size(ia)
计算两个矩阵的相同元素的总数,所有重复元素的个数求和
%%cal the overlop number in two matrix
%type_b is the std matrix & p5 is the matrix
[m n] = size(type_b)
all_sum = 0
for i =1:n
single_size = size(find(p5==type_b(i)))
all_sum = all_sum + single_size(2)
end