如何从一个更大的矩阵中提取一个2x2子矩阵 [英] How to extract a 2x2 submatrix from a bigger matrix

查看:341
本文介绍了如何从一个更大的矩阵中提取一个2x2子矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个很基本的用户,不知道太多关于在C中使用命令,所以请原谅......我不能使用非常复杂的codeS。我在stdio.h和库文件ctype.h一些知识,但多数民众赞成它。
我有一个txt文件矩阵,我要加载的矩阵根据我的行数和列数的输入

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

例如,我在文件中的5×5矩阵。我想提取特定的2×2子矩阵,我该怎么办呢?

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 ?

我创建使用嵌套循环:

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);

可悲的是code不起作用。如果我有这样的矩阵:

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

和输入3行和3列,我得到:

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

这不仅心不是一个2×2子矩阵,但即使我想第3行第3列,其无法正常打印出来....

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....

我需要3行和第3栏开始,然后坐2×2子矩阵!

I need to start at row 3 and col 3, then take the 2 by 2 submatrix !

我应该已经结束了:

4.00 23.00 
352.00 6.00

我听说,我可以使用与fgets和sscanf做到这一点。这里是我的审判code:

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 :(

我在做什么错了?

请帮忙。
谢谢!

Please help. Thanks !

推荐答案

好了,你想读大小的子矩阵的 N X M 的开始,在位置 X 的大小的大矩阵的 p X 问:的。你需要两样东西:

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. (验证的 X + N 的&LT; = P + M &LT; =的问:的)

  2. 跳到你想要阅读的矩阵的第一要素。这需要先跳过第一的的 - 1行

  3. 跳过的 X 的 - 从下一行1个元素,然后读的 N 的元素融入到您的子矩阵。重复的 M 的时间。

  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);

更新:来读取一个数组的子矩阵,而不是更简单,只是需要更多的计算。主旨在于,大小的矩阵的 P X 问:的可以存储在大小的连续排列的 P X 问:这样矩阵[I,J]可以从阵列读取[我*(J-1)+ J](约 - 有可能会关闭接一个错误,我从来不敢肯定这是列,这是行,但希望你的想法: - )

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 :-)

所以,code会是这样

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];
  }
}

这篇关于如何从一个更大的矩阵中提取一个2x2子矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆