Reputation: 41
I am new to MATLAB, and would like to understand how we can vectorize below snippet, or how can i do it efficiently:
sum=0;
for i = 1:50
sum=sum+i;
end
Upvotes: 0
Views: 41
Reputation: 11
sum(1:50)
The above statement initializes a vector of length 50 starting from 1 to 50 (with increments of length 1), and then calls MATLAB's sum function on it, returning the sum of all elements in the vector.
Upvotes: 1