「java向量拼接成矩阵」特征向量拼成的矩阵
今天给各位分享java向量拼接成矩阵的知识,其中也会对特征向量拼成的矩阵进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、如何用java程序利用二维数组创建一个矩阵,编写方法完成该矩阵的逆转,两个矩阵之和。
- 2、用JAVA编写矩阵
- 3、【Java】【矩阵】相关
- 4、怎样将几组向量变成一个矩阵
- 5、大家看一下怎样把这四个列向量组成一个矩阵
如何用java程序利用二维数组创建一个矩阵,编写方法完成该矩阵的逆转,两个矩阵之和。
两个类,一个是矩阵对象类Matrix,另一个是测试类MatrixTest
可以实现矩阵创建,赋值,转置,加法,支持行列数不同的矩阵
注1,转置方法将输出一个新的矩阵,并不改变原有矩阵的内容
注2:加法方法是a.add(b),b不发生变化,a发生变化,加法将改变a矩阵的内容,不会产生新矩阵
public class Matrix {
private int rows;
private int columns;
private Integer[][] m;
// 行列构造法
public Matrix(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.m = new Integer[rows][columns];
}
// 二维数组构造法
public Matrix(Integer[][] m) {
this.m = m;
this.rows = m.length;
this.columns = m[0].length;
}
// 转置
public Matrix exchange() {
Integer[][] result = new Integer[columns][rows];
for (int i = 0; i rows; i++) {
for (int j = 0; j columns; j++) {
result[j][i] = m[i][j];
}
}
return new Matrix(result);
}
// 加法
public void add(Matrix obj) {
if (obj.getRows() != rows || obj.getColumns() != columns) {
System.out.println("不同行列的矩阵不能相加");
} else {
Integer[][] objarray = obj.toArray();
for (int i = 0; i rows; i++) {
for (int j = 0; j columns; j++) {
m[i][j] += objarray[i][j];
}
}
}
}
// 矩阵赋值
public void setValue(int row, int column, int value) {
if (row rows column columns) {
m[row][column] = value;
} else {
System.out.println("索引超出边界");
}
}
public int getRows() {
return rows;
}
public int getColumns() {
return columns;
}
public Integer[][] toArray() {
return m;
}
public String toString() {
String result = "";
for (int i = 0; i rows; i++) {
for (int j = 0; j columns; j++) {
result = result + m[i][j] + " ";
}
result += "\r\n";
}
return result;
}
}
public class MatrixTest {
public static void main(String[] args) {
Matrix m1 = new Matrix(2, 3);
for (int i = 0; i 2; i++) {
for (int j = 0; j 3; j++) {
m1.setValue(i, j, i + j);
}
}
System.out.println(m1.toString());
System.out.println(m1.exchange().toString());
Matrix m2 = new Matrix(2, 3);
for (int i = 0; i 2; i++) {
for (int j = 0; j 3; j++) {
m2.setValue(i, j, (i + 1) * (j + 1));
}
}
System.out.println(m2.toString());
m1.add(m2);
System.out.println(m1.toString());
}
}
用JAVA编写矩阵
public static void main(String[] args) throws Exception {
print(create(getNum()));
}
private static int getNum() {
Scanner scanner = new Scanner(System.in);
int n = 0;
do {
System.out.println("请输入要生成的阶数:");
String input = scanner.next();
if (isDigital(input)) {
n = Integer.parseInt(input);
if (n = 0) {
System.out.println("阶数必须大于0");
}
}
} while (n == 0);
return n;
}
private static int[][] create(int n) {
int[][] num = new int[n][n];
int ax = 1, ay = 0, x = 0, y = 0;
for (int m = 1; m = n * n; m++) {
num[x][y] = m;
int tmpx = x + ax;
int tmpy = y + ay;
if (tmpx = n || tmpx 0 || tmpy = n || tmpy 0 || num[tmpx][tmpy] != 0) {
if (ax == 0) {
ax = -ay;
ay = 0;
} else if (ay == 0) {
ay = ax;
ax = 0;
}
}
x += ax;
y += ay;
}
return num;
}
private static void print(int[][] num) {
int length = String.valueOf(num.length * num.length).length();
for (int i = 0; i num.length; i++) {
for (int j = 0; j num[i].length; j++) {
String tmp = String.valueOf(num[i][j]);
while (tmp.length() length) {
tmp = " " + tmp;
}
System.out.print(tmp + " ");
}
System.out.println();
}
}
private static boolean isDigital(String input) {
if (input == null || input.length() == 0) return false;
for (int i = 0; i input.length(); i++) {
char ch = input.charAt(i);
if (!Character.isDigit(ch)) {
System.out.println("输入的阶数必须为数字");
return false;
}
}
return true;
}
运行时输入要生成的阶数就可以了,比如生成问题上的矩阵输入4就可以了。
【Java】【矩阵】相关
1、方法有很多这里简单交流一下我的思路,可以定义一个算法将3*3的矩阵执行某个计算或者代入到某个代数让其运算得出一个数字,对函数或者代数的要求是可逆,即通过一个数字能反推出原矩阵值。
2、可以定义查看顺序按照顺序来将矩阵内的数字全部展开。比如我们按照从上到下从左到右的顺义依次记录(1,3)(2,3)(3,3)(1,2)(2,2)......然后使用0来链接两个不同的数这样就可以将矩阵压缩为一个数字并快速还原为矩阵。
怎样将几组向量变成一个矩阵
A=[A1 A2 A3];可以合并成一个新的行向量
A=[A1;A2;A3]可以合并成一个新的矩阵,每一行就是原来的一个行向量
大家看一下怎样把这四个列向量组成一个矩阵
列向量就是只有一列的矩阵,可以用来表示向量
矩阵的乘法规则简单来说是这样的:左右两个矩阵相乘,乘得矩阵行同左,列同右,要求左列右行要相同。行由左边定,列由右边定,对应相乘以后求和为相应的数值。举个例子就明白了:
1
2
3
1
1
2
3
2
3
4
2
x
4
5
6
3
4
5
3
7
8
9
1
2
3
随便编了几个数,根据上面说的规则,新的矩阵应该是3行3列的,左面的行是3行,所以是3行,右边的列是3列,所以是3列
之后看第一行第一列,从左边找第一行,右边找第一列,对应相乘(他们的项数是相等的,都是4),第一项乘第一项1*1,第二项相乘2*4,第三项3*7,第四项1*1
然后相加为31,这就是新矩阵最左上角的数字,同理可以求得其他项,最后的结果就是
31
38
45
44
55
66
57
72
87
上面这些都是我自己写的,没有任何复制粘贴,例子也是自己出自己算的,如果可以,就选为最佳答案吧
java向量拼接成矩阵的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于特征向量拼成的矩阵、java向量拼接成矩阵的信息别忘了在本站进行查找喔。