I have matrix
p=[1 2 3 4;
5 6 7 8;
10 20 30 50];
I’d like to compute the sum of the elements of each row that are between column iMin and iMax, with iMin and iMax being different for each row
e.g for
iMin = [3 2 1];
iMax = [4 4 3];
the result is
[7 21 60]
Is there an easy Octave / Matlab way to do this without loops ?
You can achieve this using logical indexing, following a similar approach to my answer to your other question.
Assuming that
iMinandiMaxhave the same number of entries as the number of rows inpyou can horizonally tile the column indices forpi.e.[1:size(p,2)]and compare this to vertical tilings ofiMinandiMaxto generate a logical index intopfor the entries that satisfy your criterion so:Giving:
No loops 🙂