access array multiple times and calculate some value
3 views (last 30 days)
Hi all, I would like to know if there's a faster way that using a foor loop for this:
a = rng(99);
A = randn(1,100);
B = [20 23; 30 37; 51 55; 78 100 ];
For each row B, I would like to slice A from the first column to the second column value, and then apply some function, for example, sum.
So for example, the first computation would be sum(A(20:23)), then sum(A(30:37)) ... sum(A(78:100)).
The for loop would be:
for ii = 1:size(B,1)
sum(A(B(ii,1) : B(ii,1) );
Could I program this such that it results in a faster calculation?