sum of values in column wise

495 views (last 30 days)
Azza Ahmed
Azza Ahmed on 25 Jul 2012
I am trying to add values in a matrix that each is identified as I(x,y) where each of x and y is 3*3 matrix. Now, what I need to do is to sum these values in column wise and put them into a new matrix of 1*3. For example, I want to add I(x1,y1)+I(x2,y1)+I(x3,y1) and put it in first column of the new matrix. Then add I(x1,y2)+I(x2,y2)+I(x3,y2) and put this value on the second column of the new martix while keeping the number of row of the other matrix the same...and so on.
I tried to use sum(I(x,y)) with a for loop for x=1:3 while keeping the y constant and later increment the y, but this doesn't seem to work with me.
Can anyone help me with this please?
All the best

Answers (1)

Albert Yam
Albert Yam on 25 Jul 2012
B = sum(A,dim) sums along the dimension of A specified by scalar dim. The dim input is an integer value from 1 to N, where N is the number of dimensions in A. Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.
test = rand(3);
ans = sum(test,1) % sum along dimension 1
  1 Comment
Azza Ahmed
Azza Ahmed on 30 Jul 2012
Many thanks Albert for helping in this!

Sign in to comment.


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!