MATLAB: How to sum only the positive elements in a vector using an If-Statement

homeworkif statement

How do you only sum the positive elements in a vector using an if statement?

Best Answer

You can do that without if
let's say d is your array of positive and negative numbers. You want to sum only the positive ones then just do this:
sum(d(d>0))