matlab 判断元素索引_Matlab:查找具有公共元素的行索引

本文介绍了一种用于比较矩阵中各行列是否存在相同元素的算法,并通过示例展示了如何使用MATLAB或其他支持张量运算的语言实现该算法。算法的核心是利用张量比较来高效地找出矩阵中相同的元素。

考虑一下

A = [1 2 3; %Matrix A is a bit different from yours for testing

4 5 6;

7 8 1;

1 2 7;

4 5 6];

[row col] =size(A)

answers = zeros(row,row); %matrix of answers,...

%(i,j) = 1 if row_i and row_j have an equal element

for i = 1:row

for j = i+1:row %analysis is performed accounting for

% symmetry constraint

C = bsxfun(@eq,A(i,:),A(j,:)'); %Tensor comparison

if( any(C(:)) ) %If some entry is non-zero you have equal elements

answers(i,j) = 1; %output

end

end

end

answers = answers + answers'; %symmetric

这里的输出是

answers =

0 0 1 1 0

0 0 0 0 1

1 0 0 1 0

1 0 1 0 0

0 1 0 0 0

当然,答案矩阵是对称的,因为你的关系是.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值