Thread: How to sum up each column of matrices

1. How to sum up each column of matrices

HI,

Pardon me I'm not very good in looping programs and still learning every now and then....

Assuming I have a N rows by 3 columns matrix.
I have these data stored in a text file initially.
Now I wish to extract these data from the text file and perform further mathematical operations.

My main concern:

1. How to Extract the 3 columns from the text file in order to proceed to the 2nd step:

2. How do I sum up each column??

I really appreciate if anyone can help me ...

2. read everything into a 2D array of Nx3. Use a loop variable called i which starts at 0 and goes to N - 1 and within the loop add all the values of array[i][0] to get the first column total, array[i][1] to get the total of the second column, and array[i][2] to get the total of the third column.

3. Performing Mathematical operations On a Txt file from C++ Program

Hi,

I'm wondering if it's possible to perform mathematical operations, i.e. addition, on a txt file via my C++ Program?

As related to my previous thread, I have to write the compiled data (in C++) into txt file format and then to retrieve this txt file later for further use.

All I can think of is the use of:

... while (!filepointer4.eof ())
{
double vector[3];
....filepointer << vector[0] << vector[1] << vector[2];
....
I know this loop will extract the data I need from that particular file.

I'm really confused on how to sum up all data i.e. I want something like :

X = sumation of vector[0] from entire txt file
Y = sumation of vector[1] from entire txt file
Z = sumation of vector[2] from entire txt file

-----------------------------------------------------------------------------------

P.S. I appreciate all replies on my previous thread. Thank you.

4. I get the hint. ...

But can anyone give me a clearer explanation about programming wise...

I'm using something like while (!filepointer4.eof()).... as to extract the text file's data. But the adding part doesn't seem logical....

5. Code:
```int sum = 0;

...

while(something)
{
sum += Temp;
}```

6. Here's a possible schematic for reading into a 2D array from a file. This presumes you know how many values there are in the file. The exact way you'll do this depends on the exact format of the file, what you know about the file in advance, what tools you have at your disposal (for example vectors instead of arrays) etc.

Code:
```int input[3][3];

int i, j, x;

i = -1;

while( i less than 3)
{
j = 0;
increment i;
while(j less than 3)