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 ...
随机推荐
- spring mvc中,如何在 Java 代码里,获取 国际化 内容
首先,在Spring的application.xml中定义 <bean id="messageSource" class="org.springframework. ...
- 三张图较为好理解JavaScript的原型对象与原型链
最近从网上看到别人详细得讲解了js的原型对象和原型链,看完感觉是看得最清晰的一个,于是,摘录到自己博客里 对于新人来说,JavaScript的原型是一个很让人头疼的事情,一来prototype容易与_ ...
- 浪漫爱心--第三方开源--PeriscopeLayout
点此下载 使用很简单,首先在xml里面添加 <Button android:id="@+id/btn_start" android:layout_width="wr ...
- Eclipse插件开发_异常_01_java.lang.RuntimeException: No application id has been found.
一.异常现象 在运行RCP程序时,出现 java.lang.RuntimeException: No application id has been found. at org.eclipse.equ ...
- Linux-监控与安全运维之cacti
一:cacti简介 Cacti 在英文中的意思是仙人掌的意思,Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具.它通过snmpget来获取数据,使用 RR ...
- L123
My heart, the bird of the wilderness, has found its sky in your eyes. 我的心是旷野的鸟,在你的双眼中找到了天空.His main ...
- LKDBHelper Sqlite操作数据库
首先这里要说明一下,为什么用FMDB而不用Core Data呢,因为我们不知道Core Data是如何映射对象里面的属性关系的,如果我们更改了属性的话,就会报错 首先是创建LKDBHelper对象 L ...
- python主函数
Python的人会很不习惯Python没有main主函数. 这里简单的介绍一下,在Python中使用main函数的方法 #hello.py def foo(): str="function& ...
- 将变参格式化到一个string对象中
该小程序演示了变参的用法.它的功能是,仿照sprintf,将变参内容保存到string中. /* 功能说明: 仿照sprintf,将字符串格式化到一个string对象中. 实现方式: 该例子主要是用来 ...
- LeetCode 305. Number of Islands II
原题链接在这里:https://leetcode.com/problems/number-of-islands-ii/ 题目: A 2d grid map of m rows and n column ...