如何从更大的矩阵中提取 2x2 子矩阵
我是一个非常基础的用户,对 C 中使用的命令不太了解,所以请耐心等待......我不能使用非常复杂的代码。我对 stdio.h 和 ctype.h 库有一些了解,但仅此而已。 我在 txt 文件中有一个矩阵,我想根据我输入的行数和列数加载矩阵
例如,我在文件中有一个 5 x 5 矩阵。我想提取一个特定的 2 x 2 子矩阵,我该怎么做?
我使用以下方法创建了一个嵌套循环:
FILE *sample
sample=fopen("randomfile.txt","r");
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
fscanf(sample,"%f",&matrix[i][j]);
}
fscanf(sample,"\n",&matrix[i][j]);
}
fclose(sample);
遗憾的是代码不起作用..如果我有这个矩阵:
5.00 4.00 5.00 6.00
5.00 4.00 3.00 25.00
5.00 3.00 4.00 23.00
5.00 2.00 352.00 6.00
并输入 3 表示行,输入 3 表示列,我得到:
5.00 4.00 5.00
6.00 5.00 4.00
3.00 25.00 5.00
这不仅不是 2 x 2 子矩阵,而且即使我想要前 3 行和前 3 列,它没有正确打印......
我需要从第 3 行和第 3 列开始,然后采用 2 x 2 子矩阵!
我应该结束:
4.00 23.00
352.00 6.00
我听说我可以使用 fgets 和 sscanf 来完成此任务。这是我的试用代码:
fgets(garbage,1,fin);
sscanf(garbage,"\n");
但这也不起作用:(
我做错了什么?
请帮忙。 谢谢 !
I am a very basic user and do not know much about commands used in C, so please bear with me...I cant use very complicated codes. I have some knowledge in the stdio.h and ctype.h library, but thats about it.
I have a matrix in a txt file and I want to load the matrix based on my input of number of rows and columns
For example, I have a 5 by 5 matrix in the file. I want to extract a specific 2 by 2 submatrix, how can I do that ?
I created a nested loop using :
FILE *sample
sample=fopen("randomfile.txt","r");
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
fscanf(sample,"%f",&matrix[i][j]);
}
fscanf(sample,"\n",&matrix[i][j]);
}
fclose(sample);
Sadly the code does not work .. If I have this matrix :
5.00 4.00 5.00 6.00
5.00 4.00 3.00 25.00
5.00 3.00 4.00 23.00
5.00 2.00 352.00 6.00
And inputting 3 for row and 3 for column, I get :
5.00 4.00 5.00
6.00 5.00 4.00
3.00 25.00 5.00
Not only this isnt a 2 by 2 submatrix, but even if I wanted the first 3 rows and first 3 columns, its not printing it correctly....
I need to start at row 3 and col 3, then take the 2 by 2 submatrix !
I should have ended up with :
4.00 23.00
352.00 6.00
I heard that I can use fgets and sscanf to accomplish this. Here is my trial code :
fgets(garbage,1,fin);
sscanf(garbage,"\n");
But this doesnt work either :(
What am I doing wrong ?
Please help.
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,所以您想要读取大小为 n x m 的子矩阵,从位置 x、y 开始大小为 p x q 的大矩阵。您需要两件事:(
您当前的实现从矩阵的第一个元素开始读取,然后将元素连续读取到子矩阵中。更新版本:
更新:从数组读取子矩阵甚至更简单,只需要更多的计算。要点是,大小为 p x q 的矩阵可以存储在大小为 p x q 的连续数组中> 这样,可以从 array[i*(j-1)+j] 读取矩阵[i,j](大约 - 可能存在差一错误,我永远不确定哪一列是哪一列行,但希望你能明白:-)
所以代码会是这样的
OK, so you want to read a submatrix of size n x m, starting at positions x, y in the big matrix of size p x q. You need two things:
Your current implementation starts reading from the very first element of the matrix, then reads elements contiguously into the submatrix. An updated version:
Update: to read the submatrix from an array instead is even simpler, just requires a bit more calculation. The gist is, a matrix of size p x q can be stored in a contiguous array of size p x q such that matrix[i,j] can be read from array[i*(j-1)+j] (approximately - there may be off-by-one errors and I am never sure which is the column and which is the row, but hopefully you get the idea :-)
So the code would be something like
让我们分阶段来看。首先对代码进行一些小修改:
现在我们定义我们想要的内容:
现在我们忽略我们不想要的内容:
请注意,现在只有我们想要的值被复制到
matrix
中,其余的值被复制到矩阵中。matrix
是未初始化的乱码。现在将其写入result
中:编辑:
如果要从内存中已有的较大矩阵复制子矩阵,则内部循环应该是
Let's take this in stages. First a couple of minor fixes to your code:
Now we define what we want:
Now we ignore what we don't want:
Notice that now only the values we want are copied into
matrix
, the rest ofmatrix
is uninitialized gibberish. Now write it intoresult
instead:EDIT:
If you want to copy a submatrix from a larger matrix already in memory, the inner loop should be
诀窍是让编译器将特定的数组元素视为矩阵的起点;下面的代码片段可以做到这一点:
以下程序捕获了预期目的:
这是相应的输出:
The trick is to make the compiler treat your specific array element as the starting point of your matrix; the following code snippet does that:
The following program captures the intended purpose:
Here is the corresponding output: