首页 > 其他分享 >矩阵的变幻(旋转,转置,翻转,对角线,反对角线)

矩阵的变幻(旋转,转置,翻转,对角线,反对角线)

时间:2023-03-05 14:44:05浏览次数:38  
标签:11 10 begin end 转置 13 变幻 pmatrix 对角线

旋转

顺时针旋转90°(逆时针旋转270°)
点击查看代码
void  rotate_90(){		//所有矩阵适用
	swap(n,m);	//注意行列已互换
	for(int i = 1; i <= n; i ++ ){	
		for(int j = 1; j <= m; j ++ ){
			b[i][j] = a[m+1-j][i];	//注意m与j匹配
		}
	}
	for(int i = 1; i <= n; i ++ ){	//粘贴到原矩阵
		for(int j = 1; j <= m; j ++ ){
			a[i][j] = b[i][j];	
		}
	}
}

\[\begin{pmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{pmatrix} \ \ -> \ \begin{pmatrix} 7 &4 &1 \\ 8 &5 &2 \\ 9 &6 &3 \\ \end{pmatrix} \]

\[\begin{pmatrix} 1 &2 &3 &4 &5 \\ 6 &7 &8 &9 &10 \\ 11 &12 &13 &14 &15 \end{pmatrix} \ \ -> \ \begin{pmatrix} 11 &6 &1 \\ 12 &7 &2 \\ 13 &8 &3 \\ 14 &9 &4 \\ 15 &10 &5 \end{pmatrix} \]

旋转180°(顺逆一致)
点击查看代码
void rotate_180(){
	for(int i = 1; i <= n; i ++ ){	//行列不互换
		for(int j = 1; j <= m; j ++ ){
			b[i][j] = a[n+1-i][m+1-j];	//注意n与i匹配,m与j匹配
		}
	}
	for(int i = 1; i <= n; i ++ ){	//粘贴到原矩阵
		for(int j = 1; j <= m; j ++ ){
			a[i][j] = b[i][j];	
		}
	}
}

\[\begin{pmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{pmatrix} \ \ -> \ \begin{pmatrix} 9 &8 &7 \\ 6 &5 &4 \\ 3 &2 &1 \\ \end{pmatrix} \]

\[\begin{pmatrix} 1 &2 &3 &4 &5 \\ 6 &7 &8 &9 &10 \\ 11 &12 &13 &14 &15 \end{pmatrix} \ \ -> \ \begin{pmatrix} 15 &14 &13 &12 &11 \\ 10 &9 &8 &7 &6 \\ 5 &4 &3 &2 &1 \end{pmatrix} \]

顺时针旋转270°(逆时针旋转90°)
点击查看代码
void rotate_270(){
	swap(n,m);	//注意行列已互换
	for(int i = 1; i <= n; i ++ ){
		for(int j = 1; j <= m; j ++ ){
			b[i][j] = a[j][n+1-i];	//n-i
		}
	}
	for(int i = 1; i <= n; i ++ ){		//粘贴到原矩阵
		for(int j = 1; j <= m; j ++ ){
			a[i][j] = b[i][j];	
		}
	}
}

\[\begin{pmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{pmatrix} \ \ -> \ \begin{pmatrix} 3 &6 &9 \\ 2 &5 &8 \\ 1 &4 &7 \\ \end{pmatrix} \]

\[\begin{pmatrix} 1 &2 &3 &4 &5 \\ 6 &7 &8 &9 &10 \\ 11 &12 &13 &14 &15 \end{pmatrix} \ \ -> \ \begin{pmatrix} 5 &10 &15 \\ 4 &9 &14 \\ 3 &8 &13 \\ 2 &7 &12 \\ 1 &6 &11 \end{pmatrix} \]

转置

点击查看代码
void  transpose(){		
	swap(n,m);	//行列互换
	for(int i = 1; i <= n; i ++ ){	
		for(int j = 1; j <= m; j ++ ){
			b[i][j] = a[j][i];	
		}
	}
	for(int i = 1; i <= n; i ++ ){	
		for(int j = 1; j <= m; j ++ ){
			a[i][j] = b[i][j];	
		}
	}
}

\[\begin{pmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{pmatrix} \ \ -> \ \begin{pmatrix} 1 &4 &7 \\ 2 &5 &8 \\ 3 &6 &9 \\ \end{pmatrix} \]

\[\begin{pmatrix} 1 &2 &3 &4 &5 \\ 6 &7 &8 &9 &10 \\ 11 &12 &13 &14 &15 \end{pmatrix} \ \ -> \ \begin{pmatrix} 1 &6 &11 \\ 2 &7 &12 \\ 3 &8 &13 \\ 4 &9 &14 \\ 5 &10 &16 \end{pmatrix} \]

翻转

上下翻转
点击查看代码
void flip_up_down(){
	for(int i = 1; i <= n; i ++ ){		
		for(int j = 1; j <= m; j ++ ){
			b[i][j] = a[n+1-i][j];
		}
	}
	for(int i = 1; i <= n; i ++ ){		
		for(int j = 1; j <= m; j ++ ){
			a[i][j] = b[i][j];
		}
	}
}

\[\begin{pmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{pmatrix} \ \ -> \ \begin{pmatrix} 7 &8 &9 \\ 4 &5 &6 \\ 1 &2 &3 \\ \end{pmatrix} \]

\[\begin{pmatrix} 1 &2 &3 &4 &5 \\ 6 &7 &8 &9 &10 \\ 11 &12 &13 &14 &15 \end{pmatrix} \ \ -> \ \begin{pmatrix} 11 &12 &13 &14 &15 \\ 6 &7 &8 &9 &10 \\ 1 &2 &3 &4 &5 \end{pmatrix} \]

左右翻转
点击查看代码
void flip_left_right(){
	for(int i = 1; i <= n; i ++ ){		
		for(int j = 1; j <= m; j ++ ){
			b[i][j] = a[i][m+1-j];
		}
	}
	for(int i = 1; i <= n; i ++ ){		
		for(int j = 1; j <= m; j ++ ){
			a[i][j] = b[i][j];
		}
	}
}

\[\begin{pmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{pmatrix} \ \ -> \ \begin{pmatrix} 3 &2 &1 \\ 6 &5 &4 \\ 9 &8 &7 \\ \end{pmatrix} \]

\[\begin{pmatrix} 1 &2 &3 &4 &5 \\ 6 &7 &8 &9 &10 \\ 11 &12 &13 &14 &15 \end{pmatrix} \ \ -> \ \begin{pmatrix} 5 &4 &3 &2 &1 \\ 10 &9 &8 &7 &6 \\ 15 &14 &13 &12 &11 \end{pmatrix} \]

对角线翻转【相当于方阵的转置】

注意:只有方阵才有对角线

点击查看代码
void flip_diagonal(){
	for(int i = 1; i <= n; i ++ ){		
		for(int j = 1; j <= n; j ++ ){
			b[i][j] = a[j][i];
		}
	}
	for(int i = 1; i <= n; i ++ ){		
		for(int j = 1; j <= n; j ++ ){
			a[i][j] = b[i][j];
		}
	}
}

\[\begin{pmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{pmatrix} \ \ -> \ \begin{pmatrix} 1 &4 &7 \\ 2 &5 &8 \\ 3 &6 &9 \\ \end{pmatrix} \]

反对角线翻转
点击查看代码
void flip_opp_diagonal(){
	for(int i = 1; i <= n; i ++ ){		
		for(int j = 1; j <= n; j ++ ){
			b[i][j] = a[n+1-j][n+1-i];
		}
	}
	for(int i = 1; i <= n; i ++ ){		
		for(int j = 1; j <= n; j ++ ){
			a[i][j] = b[i][j];
		}
	}
}

\[\begin{pmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &9 \\ \end{pmatrix} \ \ -> \ \begin{pmatrix} 9 &6 &3 \\ 8 &5 &2 \\ 7 &4 &1 \\ \end{pmatrix} \]

标签:11,10,begin,end,转置,13,变幻,pmatrix,对角线
From: https://www.cnblogs.com/J-12045/p/17180112.html

相关文章