找一个矩阵元素在另一个矩阵的位置

找一个矩阵元素在另一个矩阵的位置可以使用MATLAB的内置函数ismember(A,B):

>>A= [1 2 3 4 5 6 10 12 13 15 16 18];
>>B = [1 3 5 10 15];
>>[tf loc] = ismember(B,A)

运行结果:

tf =1 1 1 1 1

loc =1 3 5 7 10

>> [tf loc] = ismember(A,B)

运行结果:

tf =1 0 1 0 1 0 1 0 0 1 0 0

loc =1 0 2 0 3 0 4 0 0 5 0 0

%%%%%%%%%%%%%去掉多余的行

win=[];B=[];
[tf loc] = ismember(win(:,1),B);
win(~tf,:)=[];

%%%%%%%%%%%%%去掉多余的行

该命令的功能非常强大,还可以将一个站号后面添加若干属性,比如站号列为A

A
54254
54236
54254
54237
54237

站号的属性为C:

B C
54236 9
54237 9
54254 12

运行:

[tf loc] = ismember(A,B);

D=C(loc);

[A D ];

可得:

A C
54254 12
54236 9
54254 12
54237 9
54237 9

Leave a Reply

Your email address will not be published. Required fields are marked *