如何从更大的矩阵中提取 2x2 子矩阵

发布于 2024-09-01 06:07:09 字数 1025 浏览 6 评论 0原文

我是一个非常基础的用户,对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女皇必胜 2024-09-08 06:07:09

好的,所以您想要读取大小为 n x m 的子矩阵,从位置 xy 开始大小为 p x q 的大矩阵。您需要两件事:(

  1. 验证 x + n <= py + m <= q)
  2. 跳到要读取的矩阵的第一个元素。这需要首先跳过前 y - 1 行,
  3. 跳过下一行的 x - 1 个元素,然后将 n 个元素读入子矩阵。重复m次。

您当前的实现从矩阵的第一个元素开始读取,然后将元素连续读取到子矩阵中。更新版本:

FILE *sample = fopen("randomfile.txt", "r");
// skip the first y-1 rows
for (i = 0; i < y - 1; i++) {
  fscanf(sample, "%*[^\n]\n", &matrix[i][j]);
}
for (i = 0; i < m; i++) {
  // skip the first x-1 numbers
  for (j = 0; j < x - 1; j++) {
     fscanf(sample, "%*f");
  }
  // read n numbers
  for (j = 0; j < n; j++) {
     fscanf(sample, "%f", &matrix[i][j]);
  }
  if (x + n < p) {
    // consume the rest of the line
    fscanf(sample, "%*[^\n]\n");
  }
}
fclose(sample);

更新:从数组读取子矩阵甚至更简单,只需要更多的计算。要点是,大小为 p x q 的矩阵可以存储在大小为 p x q 的连续数组中> 这样,可以从 array[i*(j-1)+j] 读取矩阵[i,j](大约 - 可能存在差一错误,我永远不确定哪一列是哪一列行,但希望你能明白:-)

所以代码会是这样的

for (i = 0; i < m; i++) {
  for (j = 0; j < n; j++) {
     submatrix[i][j] = array[(y + i) * p + x + 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:

  1. (verify that x + n <= p and y + m <= q)
  2. skip to the first element of the matrix you want to read. This requires first skipping the first y - 1 rows
  3. skip x - 1 elements from the next row, then read n elements into your submatrix. Repeat m times.

Your current implementation starts reading from the very first element of the matrix, then reads elements contiguously into the submatrix. An updated version:

FILE *sample = fopen("randomfile.txt", "r");
// skip the first y-1 rows
for (i = 0; i < y - 1; i++) {
  fscanf(sample, "%*[^\n]\n", &matrix[i][j]);
}
for (i = 0; i < m; i++) {
  // skip the first x-1 numbers
  for (j = 0; j < x - 1; j++) {
     fscanf(sample, "%*f");
  }
  // read n numbers
  for (j = 0; j < n; j++) {
     fscanf(sample, "%f", &matrix[i][j]);
  }
  if (x + n < p) {
    // consume the rest of the line
    fscanf(sample, "%*[^\n]\n");
  }
}
fclose(sample);

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

for (i = 0; i < m; i++) {
  for (j = 0; j < n; j++) {
     submatrix[i][j] = array[(y + i) * p + x + j];
  }
}
夏天碎花小短裙 2024-09-08 06:07:09

让我们分阶段来看。首先对代码进行一些小修改:

for(i=0;i<rows;i++){
  for(j=0;j<cols;j++){
    float dummy;  /* this will make thing easier later */
    fscanf(sample,"%f",&dummy);
    matrix[i][j] = dummy;
  }
/* fscanf(sample,"\n",&matrix[i][j]); this isn't even legal */
}

现在我们定义我们想要的内容:

int startrow = 2; /* The starting index. Remember we index 0,1,2,3 */
int startcol = 2;
int resultrows = 2; /* How many rows we want in our answer */
int resultcols = 2;
float result[resultrows][resultcols];

现在我们忽略我们不想要的内容:

for(i=0;i<rows;i++){
  for(j=0;j<cols;j++){
    float dummy;
    fscanf(sample,"%f",&dummy);
    if(i >= startrow && i < startrow + resultrows &&
       j >= startcol && j < startcol + resultcols){
      matrix[i][j] = dummy;
    }
  }
}

请注意,现在只有我们想要的值被复制到 matrix 中,其余的值被复制到矩阵中。 matrix 是未初始化的乱码。现在将其写入 result 中:

for(i=0;i<rows;i++){
  for(j=0;j<cols;j++){
    float dummy;
    fscanf(sample,"%f",&dummy);
    if(i >= startrow && i < startrow + resultrows &&
       j >= startcol && j < startcol + resultcols){
      result[i-startrow][j-startcol] = dummy;
    }
  }
}

编辑:
如果要从内存中已有的较大矩阵复制子矩阵,则内部循环应该是

for(j=0;j<cols;j++){
  if(i >= startrow && i < startrow + resultrows &&
     j >= startcol && j < startcol + resultcols){
      result[i-startrow][j-startcol] = matrix[i][j];
  }
}

Let's take this in stages. First a couple of minor fixes to your code:

for(i=0;i<rows;i++){
  for(j=0;j<cols;j++){
    float dummy;  /* this will make thing easier later */
    fscanf(sample,"%f",&dummy);
    matrix[i][j] = dummy;
  }
/* fscanf(sample,"\n",&matrix[i][j]); this isn't even legal */
}

Now we define what we want:

int startrow = 2; /* The starting index. Remember we index 0,1,2,3 */
int startcol = 2;
int resultrows = 2; /* How many rows we want in our answer */
int resultcols = 2;
float result[resultrows][resultcols];

Now we ignore what we don't want:

for(i=0;i<rows;i++){
  for(j=0;j<cols;j++){
    float dummy;
    fscanf(sample,"%f",&dummy);
    if(i >= startrow && i < startrow + resultrows &&
       j >= startcol && j < startcol + resultcols){
      matrix[i][j] = dummy;
    }
  }
}

Notice that now only the values we want are copied into matrix, the rest of matrix is uninitialized gibberish. Now write it into result instead:

for(i=0;i<rows;i++){
  for(j=0;j<cols;j++){
    float dummy;
    fscanf(sample,"%f",&dummy);
    if(i >= startrow && i < startrow + resultrows &&
       j >= startcol && j < startcol + resultcols){
      result[i-startrow][j-startcol] = dummy;
    }
  }
}

EDIT:
If you want to copy a submatrix from a larger matrix already in memory, the inner loop should be

for(j=0;j<cols;j++){
  if(i >= startrow && i < startrow + resultrows &&
     j >= startcol && j < startcol + resultcols){
      result[i-startrow][j-startcol] = matrix[i][j];
  }
}
遥远的她 2024-09-08 06:07:09

诀窍是让编译器将特定的数组元素视为矩阵的起点;下面的代码片段可以做到这一点:

(int(*)[SIZE_OF_2ND_DIM])(&a[4][3])

以下程序捕获了预期目的:

#include <stdio.h>

int num;

void print( int a[][num], int row, int col )
{
  int i, j;
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
      printf("%3d ", a[i][j]);
    printf("\n");
  }
}


int main()
{
  int a[10][10];
  int i, j;

  for(i = 0; i < 10; i++)
    for(j = 0; j < 10; j++)
      a[i][j] = i*10+j;

  num = 10;
  print(a, 10, 10);

  printf("\n\n");

  print((int(*)[num])(&a[4][3]), 5, 4);

  return 0;
}

这是相应的输出:

  0   1   2   3   4   5   6   7   8   9
 10  11  12  13  14  15  16  17  18  19
 20  21  22  23  24  25  26  27  28  29
 30  31  32  33  34  35  36  37  38  39
 40  41  42  43  44  45  46  47  48  49
 50  51  52  53  54  55  56  57  58  59
 60  61  62  63  64  65  66  67  68  69
 70  71  72  73  74  75  76  77  78  79
 80  81  82  83  84  85  86  87  88  89
 90  91  92  93  94  95  96  97  98  99


 43  44  45  46
 53  54  55  56
 63  64  65  66
 73  74  75  76
 83  84  85  86

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:

(int(*)[SIZE_OF_2ND_DIM])(&a[4][3])

The following program captures the intended purpose:

#include <stdio.h>

int num;

void print( int a[][num], int row, int col )
{
  int i, j;
  for(i = 0; i < row; i++)
  {
    for(j = 0; j < col; j++)
      printf("%3d ", a[i][j]);
    printf("\n");
  }
}


int main()
{
  int a[10][10];
  int i, j;

  for(i = 0; i < 10; i++)
    for(j = 0; j < 10; j++)
      a[i][j] = i*10+j;

  num = 10;
  print(a, 10, 10);

  printf("\n\n");

  print((int(*)[num])(&a[4][3]), 5, 4);

  return 0;
}

Here is the corresponding output:

  0   1   2   3   4   5   6   7   8   9
 10  11  12  13  14  15  16  17  18  19
 20  21  22  23  24  25  26  27  28  29
 30  31  32  33  34  35  36  37  38  39
 40  41  42  43  44  45  46  47  48  49
 50  51  52  53  54  55  56  57  58  59
 60  61  62  63  64  65  66  67  68  69
 70  71  72  73  74  75  76  77  78  79
 80  81  82  83  84  85  86  87  88  89
 90  91  92  93  94  95  96  97  98  99


 43  44  45  46
 53  54  55  56
 63  64  65  66
 73  74  75  76
 83  84  85  86
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文