How can I get the several sums by three interval of a matrix in matlab?

B=load('a.txt');
for i=1:length(B)
if B(i)<5
what is the next step that I can get the sum of all numbers that smaller than 5?

Answers (2)

You don't need a loop to do that:
B=load('a.txt');
sum(B(B<5))
Example:
B = [1 2 8 6 4 5 9]
>> sum(B(B<5))
ans =
7

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 10 Feb 2013

Community Treasure Hunt

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

Start Hunting!