MATLAB: Sum a column depending on another column

MATLABsum column criteria

Hi there,
I would like to sum values in a column where a criterion has been met in another column in a corresponding row of a matrix.
take the matrix:
xx 6 xx 1;
xx 9 xx 0;
xx 10 xx 1;
I want to sum the second column but only for rows where the fourth column is equal to one.
Hence, using the example, I would hope to have a solution of 16.
Any help with this simple problem is greatly appreciated.
James

Best Answer

out = sum(A(A(:,4)~=0,2))