sum of values in column wise

492 次查看(过去 30 天)
Azza Ahmed
Azza Ahmed 2012-7-25
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

回答(1 个)

Albert Yam
Albert Yam 2012-7-25
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

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by