大家好,又见面了,我是你们的朋友全栈君。
function [ out ] = inv_bin( in )
%INV_BIN Summary of this function goes here
% Detailed explanation goes here
% 计算二进制稀疏矩阵的逆
[m,n] = size(in);
if(m~=n)
fprintf('m~=n\n');
return ;
end
E = eye(m);
%%
%做行变换,变成下三角阵
for i = 1:m
noneZerosIndex = find(in(:,i)); %i到end行,第i列非零元
noneZerosIndex = noneZerosIndex(find(noneZerosIndex>=i));
if(length(noneZerosIndex)==0) %该行为0
randIndex = randi([i+1,m],1);
%列交换
temp = in(:,i);
in(:,i) = in(:,randIndex);
in(:,randIndex) = temp;
%E交换
temp = E(:,i);
E(:,i) = E(:,randIndex);
E(:,randIndex) = temp;
end
id1 = noneZerosIndex(1);
%in交换
temp = in(i,:);
in(i,:) = in(id1,:);
in(id1,:) = temp;
%E交换
temp = E(i,:);
E(i,:) = E(id1,:);
E(id1,:) = temp;
noneZerosIndex = find(in(:,i)); %第i列非零元,用其中的第一行消其他行
for cc = 1:length(noneZerosIndex)
if(noneZerosIndex(cc)~=i) %跳过第i行
temp = mod(in(noneZerosIndex(cc),:)+in(i,:) , 2);
in(noneZerosIndex(cc),:) = temp;
temp = mod(E(noneZerosIndex(cc),:)+E(i,:) , 2);
E(noneZerosIndex(cc),:) = temp;
end
end
end
out = E;
end
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/129146.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...