英文:
How to print first five elements of an matrix in Java
问题
以下是翻译好的部分:
目标是要在并行中实现以下方法:
public static double[][] parallelAddMatrix(double[][] a, double[][] b),然后在随机生成的两个大小为2000 x 2000的矩阵列表上测试我的程序。最后,我需要输出矩阵a和矩阵b的前5个元素,以及结果矩阵的前五个元素,这正是我遇到困难的地方。
这是我创建第一个和第二个矩阵的代码部分。
public static void main(String[] args) {
int var1, var2;
final int matrices = 2000;
// 创建第一个矩阵
double[][] matrixA = new double[matrices][matrices];
for(var1 = 0; var1 < matrixA.length; var1++)
for (var2 = 0; var2 < matrixA[var1].length; var2++)
matrixA[var1][var2] = 1;
// 创建第二个矩阵
double[][] matrixB = new double[matrices][matrices];
for (var1 = 0; var1 < matrixB.length; var1++)
for (var2 = 0; var2 < matrixB[var1].length; var2++)
matrixB[var1][var2] = 1;
然后稍后创建一个函数来创建结果矩阵...
public static double[][] parallelAddMatrix( double [][] a, double[][] b) {
// 创建输出矩阵
double[][] resultMatrix = new double[a.length][a[0].length];
RecursiveAction task = new multiProcess(a, b, resultMatrix);
ForkJoinPool joinPool = new ForkJoinPool();
joinPool.invoke(task);
return resultMatrix;
}
如何打印出每个矩阵的前五个元素?
我尝试了一些关于第一个和第二个矩阵的东西,比如初始化var3,然后在“matrixA(或B)[var1][var2] = 1;”下面,我放了
for (var3 = 0; var3 < 5; var3++) {
System.out.println(var3);
}
还尝试了
for (var3 = 0; var3 < 5; var3++) {
System.out.print(matrixA[var1][var2] + "");
}
System.out.println();
请在这方面提供帮助,也请告诉我应该将其放在每个代码块的哪个位置(我可能对大括号的位置有困惑)。
英文:
My goal is to implement the following method in parallel:
public static double[][] parallelAddMatrix(double[][] a, double[][] b), then test my program on randomly generated two lists of size 2000 x 2000. Finally I have to output the first 5 elements of matrix a and matrix b, and also the first five elements of the result matrix, which is what I'm having trouble with.
This is the part of my code where I create the first and second matrix.
public static void main(String[] args) {
int var1, var2;
final int matrices = 2000;
// creates first matrix
double[][] matrixA = new double[matrices][matrices];
for(var1 = 0; var1 < matrixA.length; var1++)
for (var2 = 0; var2 < matrixA[var1].length; var2++)
matrixA[var1][var2] = 1;
// creates second matrix
double[][] matrixB = new double[matrices][matrices];
for (var1 = 0; var1 < matrixB.length; var1++)
for (var2 = 0; var2 < matrixB[var1].length; var2++)
matrixB[var1][var2] = 1;
And then later created a function to create the result matrix...
public static double[][] parallelAddMatrix( double [][] a, double[][] b) {
//creates output matrix
double[][] resultMatrix = new double[a.length][a[0].length];
RecursiveAction task = new multiProcess(a, b, resultMatrix);
ForkJoinPool joinPool = new ForkJoinPool();
joinPool.invoke(task);
return resultMatrix;
}
How can I print out the first five elements for each of the three matrices?
I've tried stuff for the first and second matrix such as initializing var3, then under the "matrixA(orB)[var1][var2] = 1;", I put
for (var3 = 0; var3 < 5; var3++) {
System.out.println(var3);
}
and also tried
for (var3 = 0; var3 < 5; var3++) {
System.out.print(matrixA[var1][var2] + "");
}
System.out.println();
Please help on this, and please tell where it would be placed for each one (I might have trouble with brackets).
答案1
得分: 2
你将需要一个嵌套的for循环来迭代矩阵,并且一个计数器来查看你已经打印了多少个条目。让我们从最简单的部分开始:迭代矩阵。我将假设矩阵简单地被称为 matrix
。
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
}
}
你可能已经弄清楚了。现在我们需要一个计数器来计算我们从矩阵中打印出条目的次数。
int num_printed = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
num_printed ++;
}
}
好的。现在我们需要在达到末尾时停止。我们不能只使用一个break
语句,因为我们有两个for循环。
int num_printed = 0;
for (int i = 0; i < matrix.length; i++) { // 遍历行
for (int j = 0; j < matrix[i].length; j++) { // 遍历列
if (num_printed == 5) { // 如果我们已经打印了五个条目,停止
break;
} else { // 否则,打印下一个条目
System.out.println(matrix[i][j]);
num_printed ++; // 增加计数器
}
}
if (num_printed == 5) { // 这样我们就不会进入下一行
break;
}
}
值得注意的是,你可以创建一个独立的方法,只使用一个返回语句:
public void print_five_elements() {
int num_printed = 0;
for (int i = 0; i < matrix.length; i++) { // 遍历行
for (int j = 0; j < matrix[i].length; j++) { // 遍历列
if (num_printed == 5) { // 如果我们已经打印了五个条目,停止
return;
} else { // 否则,打印下一个条目
System.out.println(matrix[i][j]);
num_printed ++; // 增加计数器
}
}
}
}
更专业的方法
这种方法允许你使用少于五列的矩阵。然而,由于你的矩阵是2000x2000,你可以选择一个更简单的方法。将第一个索引设置为零,然后迭代到五。只需记住,如果列数少于五,这种方法将无效:
public void print_five_elements_for_wide_matrix() {
for (int i = 0; i < 5; i++) {
System.out.println(matrix[0][i]);
}
}
英文:
You'll need a nested for loop to iterate through the matrix, and a counter to see how many entries you've printed. Let's start with the easiest part: iterating over the matrix. I'll assume that the matrix is simply called matrix
.
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
}
}
You probably already figured that out. Now we need a counter to count how many times we've printed out an entry from the matrix.
int num_printed = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.println(matrix[i][j]);
num_printed ++;
}
}
Ok. So now we need to stop once we've reached the end. We can't just use one break
statement, because, we have two for loops.
int num_printed = 0;
for (int i = 0; i < matrix.length; i++) { // iterate over the rows
for (int j = 0; j < matrix[i].length; j++) { // iterate over the columns
if (num_printed == 5) { // if we've already printed five items, stop
break;
} else { // otherwise, print the next item
System.out.println(matrix[i][j]);
num_printed ++; // increment the counter
}
}
if (num_printed == 5) { // so that we don't go to the next row
break;
}
}
It's worth noting that you could create your own separate method, and only use a return statement:
public void print_five_elements() {
int num_printed = 0;
for (int i = 0; i < matrix.length; i++) { // iterate over the rows
for (int j = 0; j < matrix[i].length; j++) { // iterate over the columns
if (num_printed == 5) { // if we've already printed five items, stop
return;
} else { // otherwise, print the next item
System.out.println(matrix[i][j]);
num_printed ++; // increment the counter
}
}
}
}
More Specialized Approach
This approach allows you to use matrices that have less than five columns. However, since your matrix is 2000x2000, you could go for a much simpler approach. Use zero as the first index, and then just iterate up to five. Just keep in mind that this won't work if you have less than five columns:
public void print_five_elements_for_wide_matrix() {
for (int i = 0; i < 5; i++) {
System.out.println(matrix[0][i]);
}
}
</details>
# 答案2
**得分**: 1
由于这些矩阵的大小为 `2000 x 2000`,您不需要嵌套循环来显示每个矩阵的前 `5` 个元素。
```java
int i;
//显示 matrixA 的前 5 个元素
for(i=0; i<5; i++) {
System.out.print(matrixA[0][i] + " ");
}
System.out.println();
//显示 matrixB 的前 5 个元素
for(i=0; i<5; i++) {
System.out.print(matrixB[0][i] + " ");
}
System.out.println();
double[][] result = parallelAddMatrix(matrixA, matrixB);
//显示 result 的前 5 个元素
for(i=0; i<5; i++) {
System.out.print(result[0][i] + " ");
}
System.out.println();
需要注意的是,上述循环打印了每个矩阵第一行(即索引为 0
的行)的前 5 个元素。然而,如果您想要打印前 5 行的第一个元素,只需交换索引,例如:
System.out.println(matrixA[i][0] + " ");
英文:
Since the matrices are of size 2000 x 2000
, you do not need nested loops to display first 5
elements from each of them.
int i;
//Display first 5 elements of matrixA
for(i=0; i<5; i++) {
System.out.print(matrixA[0][i] + " ");
}
System.out.println();
//Display first 5 elements of matrixB
for(i=0; i<5; i++) {
System.out.print(matrixB[0][i] + " ");
}
System.out.println();
double[][] result = parallelAddMatrix(matrixA, matrixB);
//Display first 5 elements of result
for(i=0; i<5; i++) {
System.out.print(result[0][i] + " ");
}
System.out.println();
Note that the above loops print the first 5 elements of the first row (i.e. row at index, 0
) of each matrix. However, if you want to print the first element of the first 5 rows, just swap the indices e.g.
System.out.println(matrixA[i][0] + " ");
答案3
得分: 0
尝试这个:
将第一组括号视为行,第二组括号视为列。
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
System.out.print(matrixA[row][col] + " ");
}
System.out.println();
}
由于“多维”数组实际上是数组的数组,如果您想要打印整个矩阵,可以像这样做:
for (double[] row : matrixA) {
System.out.println(Arrays.toString(row));
}
因此,每行的长度可以不同。因此,您可能需要获取长度以按照最初的打印方式打印它们。
for (int row = 0; row < matrixA.length; row++) {
for (int col = 0; col < matrixA[row].length; col++) {
System.out.print(matrixA[row][col] + " " );
}
}
不同长度的行在“2D”数组中被称为“ragged-arrays”(不规则数组)。
英文:
Try this:
Think of the first set of brackets as the row and the second set as the column.
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
System.out.print(matrixA[row][col] + " ");
}
System.out.println();
}
Since "multi-dimensional" arrays are really arrays of arrays you can do it like this if you wanted to print out the whole matrix
for (double[] row : matrixA) {
System.out.println(Arrays.toString(row));
}
Because of this, each row can be a different length. So you may have to get the length to print them out like you first wanted to.
for (int row = 0; row < matrixA.length; row++) {
for (int col = 0; col < matrixA[row].length; col++) {
System.out.print(matrixA[row][col] + " " );
}
}
Rows of different length of a "2D" array are known as ragged-arrays
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论