C++实现矩阵的相加/相称/转置/求鞍点
1.矩阵相加
两个同型矩阵做加法,就是对应的元素相加。
#include<iostream>
using namespace std;
int main(){
int a[3][3]={{1,2,3},{6,5,4},{4,3,2}};
int b[3][3]={{4,3,2},{6,5,4},{1,2,3}};
int c[3][3]={0,0,0,0,0,0,0,0,0};
int i,j;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]+=a[i][j];//实现相加操作1
cout<<"\t"<<a[i][j];//输出矩阵A
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
c[i][j]+=b[i][j];//实现矩阵操作2
cout<<"\t"<<b[i][j];//输出矩阵B
}
cout<<endl;
}
cout<<endl;
cout<<"Array C:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cout<<"\t"<<c[i][j];//输出矩阵C
}
cout<<endl;
}
cout<<endl;
return 0; }
2.实现矩阵的转置
#include<iostream>
using namespace std;
int main(){
int a[3][2]={{4,3},{6,5},{1,2}};
int b[2][3]={0,0,0,0,0,0};
int i,j;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<2;j++){
cout<<"\t"<<a[i][j];//输出矩阵A
b[j][i]=a[i][j];//进行转置操作
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<"\t"<<b[i][j];
}
cout<<endl;
}
cout<<endl;
return 0; }
3.实现矩阵的相乘
一个m行n列的矩阵可以和n列k行的矩阵相乘,得到一个m行k列的矩阵
#include<iostream>
using namespace std;
int main(){
int a[3][2]={{4,3},{6,5},{1,2}};
int b[2][3]={{1,2,3},{6,5,4}};
int c[3][3]={0,0,0,0,0,0,0,0,0};
int i,j,k,l;
cout<<"Array A:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<2;j++){
cout<<"\t"<<a[i][j];//输出矩阵A
}
cout<<endl;
}
cout<<endl;
cout<<"Array B:"<<endl;
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<"\t"<<b[i][j];//输出矩阵B
}
cout<<endl;
}
cout<<endl;
cout<<"Array C:"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(k=0;k<2;k++){
c[i][j]+=a[i][k]*b[k][j];//实现相乘操作
}
cout<<"\t"<<c[i][j];//输出矩阵C
}
cout<<endl;
}
cout<<endl;
return 0; }
4.求矩阵中的鞍点
在矩阵中行中最大,列中最小的元素就是我们要求的鞍点
#include<iostream>
using namespace std;
int main(){
int a[3][4]={{3,2,13,1},{8,7,10,5},{12,11,14,9}};
int i,j,k,ad,q=0;
bool tag;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
cout<<endl;
for(i=0;i<3;i++){
ad=a[i][0];
tag=true;
for(j=1;j<4;j++){
if(ad<a[i][j]){
k=j;
}//先选出行中最大
}
for(j=0;j<3;j++){
if(a[i][k]>a[j][k]){
tag=false;
};//再选出列中最小
}
cout<<endl;
if(tag==true){
cout<<"鞍点是第"<<(i+1)<<"行,第"<<(k+1)<<"列的"<<a[i][k]<<endl;
q++;
}
}
if(q==0){
cout<<"没有一个鞍点~"<<endl;
}
cout<<endl;
return 0; }
C++实现矩阵的相加/相称/转置/求鞍点的更多相关文章
- 矩阵的f范数及其求偏导法则
转载自: http://blog.csdn.net/txwh0820/article/details/46392293 矩阵的迹求导法则 1. 复杂矩阵问题求导方法:可以从小到大,从scalar到 ...
- 矩阵的 Frobenius 范数及其求偏导法则
cr:http://blog.csdn.net/txwh0820/article/details/46392293 一.矩阵的迹求导法则 1. 复杂矩阵问题求导方法:可以从小到大,从scalar到 ...
- 关于matlab矩阵卷积conv2和傅里叶变换求卷积ifft2的关系
先定义两个矩阵 a = [1 2 3 5 ; 4 7 9 5;1 4 6 7;5 4 3 7;8 7 5 1] %a矩阵取5*4 b = [1 5 4; 3 6 8; 1 5 7] %b矩阵如多数 ...
- 螺旋矩阵O(1)根据坐标求值
传送门 洛谷2239 •题意 从矩阵的左上角(第11行第11列)出发,初始时向右移动: 如果前方是未曾经过的格子,则继续前进,否则右转: 重复上述操作直至经过矩阵中所有格子. 根据经过顺序,在格子中依 ...
- golang 矩阵乘法、行列式、求逆矩阵
package matrix import ( "math" "github.com/astaxie/beego" ) type Matrix4 struct ...
- 矩阵的frobenius范数及其求偏导法则
例子: http://www.mathchina.net/dvbbs/dispbbs.asp?boardid=4&Id=3673
- hdu 1005 Number Sequence(矩阵连乘+二分快速求幂)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1005 代码: #include<iostream> #include<stdio.h&g ...
- C语言算法---求鞍点
题目:有一个3X4矩阵,要求输出其鞍点(行列均最大的值),以及它的行号和列号. int a[3][4] = {{123,94,-10,218}, {3 ...
- [zt]矩阵求导公式
今天推导公式,发现居然有对矩阵的求导,狂汗--完全不会.不过还好网上有人总结了.吼吼,赶紧搬过来收藏备份. 基本公式:Y = A * X --> DY/DX = A'Y = X * A --&g ...
随机推荐
- Element 'beans' cannot have character [children], because the type's content type is element-only
这个小问题快搞死我了,找了大半个小时. Element 'beans' cannot have character [children], because the type's content typ ...
- CentOS 6.8安装Docker V1.0
rpm -Uvh http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm yum -y install do ...
- JDBC操作简单实用了IOUtils
package cn.itcast.demo4; import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...
- Django REST_framework Quickstart
局部避免crsf的方式 针对视图函数: from django.views.decorators.csrf import csrf_exempt @csrf_exempt def foo(reques ...
- python中join函数用法
str.join(list/tuple/dict/string) str = "-"; seq = ("a", "b", "c&q ...
- Reinforcement Learning Q-learning 算法学习-1
- rpy2的安装问题?【解决】
https://www.zhihu.com/question/46555829 http://blog.sciencenet.cn/blog-365459-901335.html
- LeetCode 293. Flip Game
原题链接在这里:https://leetcode.com/problems/flip-game/description/ 题目: You are playing the following Flip ...
- centos安装教程
centos7和6.x有很大的差别,在第一次安装的时候遇到了很多坑,一共安装了三次才成功 如果是用window自带的Hyper-v装centos时 安装包在\\10.10.10.1\ShareDo ...
- bzoj 4503 两个串——FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4503 翻转T,就变成卷积.要想想怎么判断. 因为卷积是乘积求和,又想到相等的话相减为0,所以 ...