MATLAB: Selecting a range of rows at a time

MATLABselecting a range of rows

I have a matrix A = 1201×50. I would like to select a range of 30 rows at a time to analyse it (1-30, 31-60, 61-90…).
To explain better: I would like to find the bandpower of every 30 rows in all 50 columns (end up with a 40 x 50).
The easiest but very time consuming to write would be to select every single row range:
fs = 300
freqrange = [7 11]
Aa = bandpower(A((1:30),:),fs,freqrange);
Ab = bandpower(A((31:60),:),fs,freqrange);
Ac = bandpower(A((61:90),:),fs,freqrange);
Ad = bandpower(A((91:120),:),fs,freqrange);
(....)
FPOWER_Reward_7_11 = [Aa;Ab;Ac;Ad...]
But since I'll have to run a lot of frequency ranges, I would like a faster way of running this, without having to write each range of 30 (it will be 40 lines per frequency range). Is there a way to make this row selection easier and faster?
Thank you!

Best Answer

Method one: use a loop and basic indexing: this is the easiest and most efficient solution:
out = NaN(40,50);
for k = 1:40;
idx = (k-1)*30+(1:30);
out(k,:) = bandpower(A(idx,:),fs,freqrange);
end
Method Two: use mat2cell and cellfun:
C = mat2cell(A,30*ones(1,40),50);
out = cell2mat(cellfun(@(m)bandpower(m,fs,freqrange),C,'uni',0));
Method Three: use accumarray:
idx = 1:size(A,1);
grp = ceil(idx(:)/30);
accumarray(grp,idx(:),[],@(r)bandpower(A(r,:),fs,freqrange));
Warning: whatever you do, do NOT try to create or access lots of variable names (e.g. Aa, Ab, etc) in a loop. Read this to know why: