我的矩阵在可视化和分配中是不一样的?(My matrix is not the same in visualization and in allocation?)

编程入门 行业动态 更新时间:2024-10-19 15:40:19
我的矩阵在可视化和分配中是不一样的?(My matrix is not the same in visualization and in allocation?)

作为C的新手,我写了一些代码来制作三对角矩阵。 但是我很茫然,因为这个矩阵在初始化和可视化之间变化。 使用printf查看矩阵“在纸上”,我甚至打印了索引i和j以检查那里是否存在问题,但显然没有。 我不明白A [2,2]如何从2变为-1,你能帮我吗? 这是我的输出:

[0][0]: 2.000000 [0][1]: -1.000000 [0][2]: 0.000000 [0][3]: 0.000000 [1][0]: -1.000000 [1][1]: 2.000000 [1][2]: -1.000000 [1][3]: 0.000000 [2][0]: 0.000000 [2][1]: -1.000000 [2][2]: 2.000000 [2][3]: -1.000000 [3][0]: 0.000000 [3][1]: 0.000000 [3][2]: -1.000000 [3][3]: 2.000000 [0][0]: 0.000000 [0][1]: 0.000000 [0][2]: -1.000000 [0][3]: 2.000000 [1][0]: 0.000000 [1][1]: 0.000000 [1][2]: -1.000000 [1][3]: 2.000000 [2][0]: 0.000000 [2][1]: 0.000000 [2][2]: -1.000000 [2][3]: 2.000000 [3][0]: 0.000000 [3][1]: 0.000000 [3][2]: -1.000000 [3][3]: 2.000000

虽然我的代码看起来像这样:

float **A; int i, j = 0; A_row = (float *)malloc( N * sizeof(float)); A = (float **)malloc( N * sizeof(float *)); for (i = 0; i < N; i++){ A[i] = A_row; } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { A[i][j] = 0.; if(i == j){A[i][j] = 2.;} else if(i == j-1) {A[i][j]=-1;} else if(i == j+1) {A[i][j]=-1;} printf("[%d][%d]: %f ", i, j, A[i][j]); } printf("\n"); } //check A for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("[%d][%d]: %f ", i, j, A[i][j]); } printf("\n"); }

我真的很抱歉这个麻烦。

Being new to C, I wrote a little code to make a tri-diagonal matrix. However I'm at a loss, since this matrix changes between initialization and visualization. Using printf to see the matrix "as on paper", I even printed the indices i and j to check if there was an issue there, but apparently not. I do not understand how A[2, 2] changes from 2 to -1, could you please help me? Here is the output I have:

[0][0]: 2.000000 [0][1]: -1.000000 [0][2]: 0.000000 [0][3]: 0.000000 [1][0]: -1.000000 [1][1]: 2.000000 [1][2]: -1.000000 [1][3]: 0.000000 [2][0]: 0.000000 [2][1]: -1.000000 [2][2]: 2.000000 [2][3]: -1.000000 [3][0]: 0.000000 [3][1]: 0.000000 [3][2]: -1.000000 [3][3]: 2.000000 [0][0]: 0.000000 [0][1]: 0.000000 [0][2]: -1.000000 [0][3]: 2.000000 [1][0]: 0.000000 [1][1]: 0.000000 [1][2]: -1.000000 [1][3]: 2.000000 [2][0]: 0.000000 [2][1]: 0.000000 [2][2]: -1.000000 [2][3]: 2.000000 [3][0]: 0.000000 [3][1]: 0.000000 [3][2]: -1.000000 [3][3]: 2.000000

While my code looks like this:

float **A; int i, j = 0; A_row = (float *)malloc( N * sizeof(float)); A = (float **)malloc( N * sizeof(float *)); for (i = 0; i < N; i++){ A[i] = A_row; } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { A[i][j] = 0.; if(i == j){A[i][j] = 2.;} else if(i == j-1) {A[i][j]=-1;} else if(i == j+1) {A[i][j]=-1;} printf("[%d][%d]: %f ", i, j, A[i][j]); } printf("\n"); } //check A for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("[%d][%d]: %f ", i, j, A[i][j]); } printf("\n"); }

I'm really sorry for the trouble.

最满意答案

矩阵中的所有行都相同,它们将等于最后一行。

A[i] = A_row

应该

A[i] = malloc(N * sizeof(float));

即为每一行分配N float ,就像你拥有它一样,你为每一行分配“ 相同的空间 ”。 改变该空间的内容将同时影响所有行( 或者不是真的,因为实际上它们是同一 )。

All the rows in your matrix are the same, they will be equal to the last one.

A[i] = A_row

should be

A[i] = malloc(N * sizeof(float));

i.e. allocate N floats for each row, as you have it you are assigning the "same space" for every row. Altering the contents of that space will affect all the rows simultaneously (or not really, because in fact they are the same row).

更多推荐

printf,电脑培训,计算机培训,IT培训"/> <meta name="description" cont

本文发布于:2023-08-06 22:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1456633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   分配   中是不   allocation   visualization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!