矩阵转置(压缩/快速)

矩阵转置

public class threeGroup {
    /* 定义辅助数组 */
    private final int[] num = new int[30];

    /* 每列上非零元素的个数 */
    private final int[] pos = new int[30];

    /**
     * 主函数
     * @param args ...
     */
    public static void main(String[] args) {
        new threeGroup().control();
    }

    /**
     * control
     */
    private void control(){
        xx x = new xx();
        x.setRowNum(6);
        x.setColumnNum(6);
        x.setNumN(8);
        /* 初始化对象 */
        Init(x);
        /* 初始化辅助数组 */
        for(int i = 0;i < num.length;i++){
            num[i] = 0;
            pos[i] = 0;
        }
        /* 添加数据 */
        {
            x.cc[1].setRow(1);
            x.cc[1].setColumn(2);
            x.cc[1].setValue(12);

            x.cc[2].setRow(1);
            x.cc[2].setColumn(3);
            x.cc[2].setValue(9);

            x.cc[3].setRow(3);
            x.cc[3].setColumn(1);
            x.cc[3].setValue(-3);

            x.cc[4].setRow(3);
            x.cc[4].setColumn(5);
            x.cc[4].setValue(14);

            x.cc[5].setRow(4);
            x.cc[5].setColumn(3);
            x.cc[5].setValue(24);

            x.cc[6].setRow(5);
            x.cc[6].setColumn(2);
            x.cc[6].setValue(18);

            x.cc[7].setRow(6);
            x.cc[7].setColumn(1);
            x.cc[7].setValue(15);

            x.cc[8].setRow(6);
            x.cc[8].setColumn(4);
            x.cc[8].setValue(-7);
        }
        count(x);
        print();

        System.out.println("\n\n\n| 转置前:");
        put(x);

        System.out.println("\n| 转置后:");
        quickZhuanzhi(x);

    }

    /**
     * 输出矩阵
     * @param x 存储矩阵中有值的元素
     */
    private void put(xx x){
        for(int i = 1;i <= x.getNumN();i++){
            System.out.println("(" + x.cc[i].getRow() + "," + x.cc[i].getColumn() + "," + x.cc[i].getValue() + ")");
        }
    }

    /**
     * 初始化
     * @param x ...
     */
    private void Init(xx x){
        for(int i = 0;i < x.cc.length;i++){
            x.cc[i] = new node();
        }
    }

    /**
     * 压缩转置
     * @param x ...
     */
    private void zhuanzhi(xx x){
        int min;
        int z;

        /* 先对原来三元组中的数据的行类序号进行转置 */
        for(int i = 1;i <= x.getNumN();i++){
            z = x.cc[i].getColumn();
            x.cc[i].setColumn(x.cc[i].getRow());
            x.cc[i].setRow(z);
        }

        /* 循环对三元组中的数据进行选择排序 */
        for(int i = 1;i <= x.getNumN();i++){
            min = i;
            node d;

            /* 寻找下一个最小的三元组 */
            for(int j = i + 1;j <= x.getNumN();j++){
                if(x.cc[min].getRow() > x.cc[j].getRow()){
                    min = j;
                }else if(x.cc[min].getRow() == x.cc[j].getRow() && x.cc[min].getColumn() > x.cc[j].getColumn()){
                    min = j;
                }
            }
            /* 交换 */
            if(min != i){
                d = x.cc[min];
                x.cc[min] = x.cc[i];
                x.cc[i] = d;
            }
        }
    }

    /**
     * 快速转置
     * @param x wu
     */
    private void quickZhuanzhi(xx x){
        /* 创建对象,用于存储排序好的三元组 */
        xx b = new xx();
        /* 录入初始数据 */
        b.setRowNum(x.getColumnNum());
        b.setColumnNum(x.getRowNum());
        b.setNumN(x.getNumN());

        /* 初始化对象 */
        Init(b);

        int a,c;
        for(int i = 1;i <= x.getNumN();i++){
            /* 获取列号 */
            c = x.cc[i].getColumn();
            /* 获取此元素在新的对象中的序号 */
            a = pos[c];
            /* 添加数据 */
            b.cc[a].setRow(x.cc[i].getColumn());
            b.cc[a].setColumn(x.cc[i].getRow());
            b.cc[a].setValue(x.cc[i].getValue());
            /* 后移 */
            ++pos[c];
        }
        /* 输出 */
        put(b);
    }

    /**
     * 计算辅助数组
     * @param x ...
     */
    private void count(xx x){
        int a;
        for(int i = 1;i <= x.getNumN();i++){
            a = x.cc[i].getColumn();
            num[a] += 1;
        }
        pos[1] = 1;
        for(int i = 2;i <= x.getColumnNum();i++){
            pos[i] = pos[i - 1] + num[i - 1];
        }
    }

    /**
     * 输出辅助数组里的数据
     */
    private void print(){
        for(int i = 1;i <= 6;i++){
            System.out.print(num[i] + " ");
        }
        System.out.println();
        for(int i = 1;i <= 6;i++){
            System.out.print(pos[i] + " ");
        }

    }
}
final class xx{

    /* 值结点类型 */
    node[] cc = new node[20];
    /* 行数 */
    private int rowNum;
    /* 列数 */
    private int columnNum;
    /* 非零元个数 */
    private int numN;

    public int getColumnNum() {
        return columnNum;
    }

    public void setColumnNum(int columnNum) {
        this.columnNum = columnNum;
    }

    public int getNumN() {
        return numN;
    }

    public void setNumN(int numN) {
        this.numN = numN;
    }

    public int getRowNum() {
        return rowNum;
    }

    public void setRowNum(int rowNum) {
        this.rowNum = rowNum;
    }
}

final class node{

    /* 行号 */
    private int row;
    /* 列号 */
    private int column;
    /* 值 */
    private int value;

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}


  • 记录-以供复习
  • 多有不足,多谢指正!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最向往的地方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值