How can I get the several sums by three interval of a matrix in matlab?
Show older comments
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)
Shashank Prasanna
on 10 Feb 2013
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
Walter Roberson
on 10 Feb 2013
total = total + B(i)
Do not forget to initialize the total.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!