create a conditional summation in a loop where the condition to add a term in the summation is that c(ii) == 3. in matlab

You can create a loop to iterate over the elements of the vector and add the terms that match the condition c(ii) == 3. The code below demonstrates how to do this:

main.m
c = [1 2 3 4 5];
sum = 0;

for ii=1:length(c)
    if c(ii) == 3
        sum = sum + c(ii);
    end
end

disp(sum);
115 chars
11 lines

In this example, we initialize the vector c, the variable sum to 0, and then iterate over the elements of c using a for loop. For each element of c, we check if the condition c(ii) == 3 is true. If it is, we add the current element to sum. Finally, we display the value of sum.

gistlibby LogSnag