Print Matrix Diagonally

本文介绍了一种用于打印二维矩阵中的元素沿着两条不同方向的对角线(从右上到左下及从左上到右下)的算法实现。通过两层循环遍历矩阵,该算法能够有效地按对角线顺序输出所有元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class PrintDiagonally {

	public static void main(String[] args) {
		int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
		printDiagonally(matrix);
	}

	public static void printDiagonally(int[][] m) {
		if (m == null || m.length == 0 || m[0].length == 0)
			return;
		int rows = m.length, columns = m[0].length;

		// 右上至左下
		for (int j = columns - 1; j >= 0; j--) {
			for (int i = 0; i < rows; i++)
				if (j + i < columns)
					System.out.print(m[i][i + j] + " ");
			System.out.println();
		}
		for (int i = 1; i < rows; i++) {
			for (int j = 0; j < columns; j++)
				if (i + j < rows)
					System.out.print(m[i + j][j] + " ");
			System.out.println();
		}
		System.out.println("=============");

		// 左上至右下
		int maxSum = rows + columns - 2;
		for (int sum = 0; sum <= maxSum; sum++) {
			for (int i = 0; i <= sum; i++) {
				if (i < rows && (sum - i) < columns) {
					System.out.print(m[i][sum - i] + " ");
				}
			}
			System.out.println();
		}
	}
}

帮我解释这个关于LDPC的函数 function [c, newH] = makeParityChk(dSource, H, strategy) % Generate parity check vector bases on LDPC matrix H using sparse LU decomposition % % dSource : Binary source (0/1) % H : LDPC matrix % strategy: Strategy for finding the next non-zero diagonal elements % {0} First : First non-zero found by column search % {1} Mincol : Minimum number of non-zeros in later columns % {2} Minprod: Minimum product of: % - Number of non-zeros its column minus 1 % - Number of non-zeros its row minus 1 % % c : Check bits % % % Copyright Bagawan S. Nugroho, 2007 % http://bsnugroho.googlepages.com % Get the matric dimension [M, N] = size(H); % Set a new matrix F for LU decomposition F = H; % LU matrices L = zeros(M, N - M); U = zeros(M, N - M); % Re-order the M x (N - M) submatrix for i = 1:M % strategy {0 = First; 1 = Mincol; 2 = Minprod} switch strategy % Create diagonally structured matrix using 'First' strategy case {0} % Find non-zero elements (1s) for the diagonal [r, c] = find(F(:, i:end)); % Find non-zero diagonal element candidates rowIndex = find(r == i); % Find the first non-zero column chosenCol = c(rowIndex(1)) + (i - 1); % Create diagonally structured matrix using 'Mincol' strategy case {1} % Find non-zero elements (1s) for the diagonal [r, c] = find(F(:, i:end)); colWeight = sum(F(:, i:end), 1); % Find non-zero diagonal element candidates rowIndex = find(r == i); % Find the minimum column weight [x, ix] = min(colWeight(c(rowIndex))); % Add offset to the chosen row index to match the dimension of the... % original matrix F chosenCol = c(rowIndex(ix)) + (i - 1); % Create
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值