前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >二元域矩阵求逆MATLAB代码

二元域矩阵求逆MATLAB代码

作者头像
全栈程序员站长
发布2022-07-29 15:09:55
2980
发布2022-07-29 15:09:55
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/129146.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档