I'm stuck here. I've got a matrix of size NxN stored in a double array. Then I want to delete a given column, lets say the first column. So I created a new double array of size NxN-1 and copy the values from the first matrix to the second one, except the 1st column of course. But then I want to set the first array to be the second array. I am blanking here.
我困在这里。我有一个大小为NxN的矩阵存储在一个双数组中。然后我要删除给定的列,假设是第一列。所以我创建了一个新的双数组大小为NxN-1并将值从第一个矩阵复制到第二个矩阵,当然第一列除外。然后我想把第一个数组设为第二个数组。我这里消隐。
double matrix[N][N]
//fill up the matrix code here...
// remove first column of array
double newMatrix[N][N-1];
for(i = 0; i < N; i++){
for(j = 1; j < N; j++){
newMatrix[i][j-1] = matrix[i][j];
}
}
matrix = newMatrix; // how do I set this correctly? Do I need to realloc the first array?
3 个解决方案
#1
2
You cannot assign arrays in C, which I assume that your compiler tells you. To do such dynamic memory management, you will need to use pointers instead of arrays. I suggest you read up on how malloc()
and free()
work so that you can do what you want.
不能在C中分配数组,我假设编译器会告诉你。要执行这种动态内存管理,需要使用指针而不是数组。我建议您阅读malloc()和free()是如何工作的,以便您可以做您想做的事情。
Edit:
编辑:
Another solution comes to mind if you are only removing columns (or rows): keep track of the number of rows and columns used in the array. Then you can remove a row or column within the original array without creating a copy first. Just move the data past the delete column (or row) to the left (or up) then decrement your size counters. (I hope this make sense. If not let me know and I'll elaborate.)
如果您只删除列(或行):跟踪数组中使用的行和列的数量,就会想到另一个解决方案。然后,您可以删除原始数组中的一行或列,而无需首先创建副本。只需将数据移动到delete列(或行)左边(或上面),然后递减大小计数器。我希望这是有意义的。如果不告诉我,我会详细说明。
#2
1
like Code-guru said malloc() and free() should help alot, but if u simply wanted to delete the last column the you wouldn't need two arrays:
像代码大师说的malloc()和free()应该可以帮助alot,但是如果你只是想删除最后一个列,你就不需要两个数组:
double matrix[2][3] = {1,2,3,4,5,6}; //declaring a 2 by 3 matrix
for (i=0;i<2;i++) //rows
{
for (j=0;j<3-1;j++) //columns - 1
{
printf("%.1f ",matrix[i][j]); //I chose to display matrix...
}
printf("\n");
}
#3
1
Instead of accessing elements from array[i][j], one might opt to access elements from array + stride_x[x] + stride_y[y]
; where array is originally introduced as double matrix[N*N];
or double *matrix = malloc(sizeof(double)*N*N);
.
与其访问数组[i][j]中的元素,还可以选择从数组+ stride_x[x] + stride_y[y]中访问元素;其中数组最初引入为双矩阵[N*N];或双*矩阵= malloc(sizeof(double)*N*N);
The stride_y[x] would originally contain offsets of columns for all rows: 0 1 2 3 4 ... N-1 and stride_y[y] would contain similar offsets multiplied with original row width 0 N 2*N 3*N..
stride_y[x]最初将包含所有行的列偏移量:0 1 2 3 4…N-1和stride_y[y]将包含与原始行宽度0 N 2*N 3*N相同的偏移量。
From these 1-D arrays one can more effortlessly delete or exchange complete rows and columns, which may come handy in eg. recursive implementation of determinant calculation / Gauss Jordan elimination.
从这些一维数组中,可以更轻松地删除或交换完整的行和列,这在eg中可能很有用。递归实现行列式计算/高斯约当消。
#1
2
You cannot assign arrays in C, which I assume that your compiler tells you. To do such dynamic memory management, you will need to use pointers instead of arrays. I suggest you read up on how malloc()
and free()
work so that you can do what you want.
不能在C中分配数组,我假设编译器会告诉你。要执行这种动态内存管理,需要使用指针而不是数组。我建议您阅读malloc()和free()是如何工作的,以便您可以做您想做的事情。
Edit:
编辑:
Another solution comes to mind if you are only removing columns (or rows): keep track of the number of rows and columns used in the array. Then you can remove a row or column within the original array without creating a copy first. Just move the data past the delete column (or row) to the left (or up) then decrement your size counters. (I hope this make sense. If not let me know and I'll elaborate.)
如果您只删除列(或行):跟踪数组中使用的行和列的数量,就会想到另一个解决方案。然后,您可以删除原始数组中的一行或列,而无需首先创建副本。只需将数据移动到delete列(或行)左边(或上面),然后递减大小计数器。我希望这是有意义的。如果不告诉我,我会详细说明。
#2
1
like Code-guru said malloc() and free() should help alot, but if u simply wanted to delete the last column the you wouldn't need two arrays:
像代码大师说的malloc()和free()应该可以帮助alot,但是如果你只是想删除最后一个列,你就不需要两个数组:
double matrix[2][3] = {1,2,3,4,5,6}; //declaring a 2 by 3 matrix
for (i=0;i<2;i++) //rows
{
for (j=0;j<3-1;j++) //columns - 1
{
printf("%.1f ",matrix[i][j]); //I chose to display matrix...
}
printf("\n");
}
#3
1
Instead of accessing elements from array[i][j], one might opt to access elements from array + stride_x[x] + stride_y[y]
; where array is originally introduced as double matrix[N*N];
or double *matrix = malloc(sizeof(double)*N*N);
.
与其访问数组[i][j]中的元素,还可以选择从数组+ stride_x[x] + stride_y[y]中访问元素;其中数组最初引入为双矩阵[N*N];或双*矩阵= malloc(sizeof(double)*N*N);
The stride_y[x] would originally contain offsets of columns for all rows: 0 1 2 3 4 ... N-1 and stride_y[y] would contain similar offsets multiplied with original row width 0 N 2*N 3*N..
stride_y[x]最初将包含所有行的列偏移量:0 1 2 3 4…N-1和stride_y[y]将包含与原始行宽度0 N 2*N 3*N相同的偏移量。
From these 1-D arrays one can more effortlessly delete or exchange complete rows and columns, which may come handy in eg. recursive implementation of determinant calculation / Gauss Jordan elimination.
从这些一维数组中,可以更轻松地删除或交换完整的行和列,这在eg中可能很有用。递归实现行列式计算/高斯约当消。