I am looking for a better way to calculate the sum off all elements from index X to index Y, if I have a list of indexes.
For example:
a = {1, 40, 77} % indexes
cell_elements = {1,1,1.....,1} %100 elements, each 1
My current idea looks something like this:
counter = 1;
for k=1:length(cell_elements)
if(%Some condition) %condition is true for k=1,40 and 77
sum = sum+cell_elements(k);
result(counter) = sum;
sum=0;
counter = counter+1;
else
sum = sum+cell_elements(k);
end
end
I would like to improve the code, since I have the feeling that the problematic is simple, but due to my lack of experience in matlab my code is too long.
Is there any function, where I could just pass a list of indexes and it will do the same job as the code above?