sum values of the elements in vector that are lower than 26 without using the sum command in matlab

Here's a solution that uses conditional statements, loops, and array indexing to sum the values of the elements in a vector that are lower than 26:

main.m
% Example vector
v = [10 20 30 15 25 40];

% Initialize sum variable
s = 0;

% Loop through vector
for i = 1:length(v)
    
    % Check if element is lower than 26
    if v(i) < 26
        
        % Add element to sum
        s = s + v(i);
    end
    
end

% Display the sum
disp(s)
285 chars
21 lines

This code loops through the elements in the vector, checks if each element is lower than 26 with an if statement, and adds it to the sum variable if it is. The sum is then displayed at the end. Note that the length() function is used to determine the number of elements in the vector.

gistlibby LogSnag