Selecting a range of rows at a time
Show older comments
I have a matrix A = 1201x50. 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!
Accepted Answer
More Answers (2)
Jos (10584)
on 14 Apr 2017
You can loop over the rows, using for-loops or arrayfun:
step = 30 ;
fh = @(x) bandpower(A(x:min(x+step-1,size(A,2)),:), fs, freqrange);
Y = arrayfun(fh, 1:30step:size(A,2), 'un', 0)
FPOWER_Reward_7_11 = cat(1, Y{:})
Rik
on 14 Apr 2017
You can replace your code with the code below, although it will not be any faster than the code you describe.
fs = 300
freqrange = [7 11]
FPOWER_Reward_7_11 = zeros(40,50);
for n=1:40
FPOWER_Reward_7_11(n,:) = bandpower(A((1:30)+30*(n-1),:),fs,freqrange);
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!