sum of values in column wise

276 visualizzazioni (ultimi 30 giorni)
Azza Ahmed
Azza Ahmed il 25 Lug 2012
Hi,
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
AA

Risposte (1)

Albert Yam
Albert Yam il 25 Lug 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 Commento
Azza Ahmed
Azza Ahmed il 30 Lug 2012
Many thanks Albert for helping in this!
Cheers
AA

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by