简体   繁体   English

C ++中矩阵的加法

[英]addition in matrix in c++

following is the code 以下是代码

for (int w=0;w<10;w++)
        {
              for( int y=0;y<8;y++)
            {
                    matrix[y][0] = arr_v1[y];
                    matrix[y][1] = arr_v2[y];
                    matrix[y][2] = arr_v3[y];
                    matrix[y][3] = arr_v4[y];
                    matrix[y][4] = arr_v5[y];
                    matrix[y][5] = arr_v6[y];
                    matrix[y][6] = arr_v7[y];
                    matrix[y][7] = arr_v8[y];
            }
            }

i want to add the values to matrix every time, for loop, for (int w=0;w<10;w++) runs. 我想每次(循环w = 0; w <10; w ++)运行时将值添加到矩阵中进行循环。 like, when w=0, it will first put values in the matrix, next time when w=1 runs, it should add values to the same matrix and so on. 例如,当w = 0时,它将首先将值放在矩阵中,下次当w = 1运行时,应将值添加到同一矩阵中,依此类推。 I am not sure, but probably something like: 我不确定,但可能类似:

int add_val=0;
        for(int c=0;c<8;c++)
        {
            for(int d=0;d<8;d++)
            {
                add_val+=matrix[c][d];
            cout<<matrix[c][d]<<" "; 
            }
            cout<<"\n";
        }

Init your matrix values with zeros when creating or before the loop. 在创建循环时或循环之前,将矩阵值初始化为零。 Then just add values in your loop 然后在循环中添加值

You can initialize all of your matrix cells to zero, then you can write something like - 您可以将所有矩阵单元初始化为零,然后可以编写类似-

for(int w=0; w<10; w++)
........
    matrix[index1][index2] += your_value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM