MATLAB: Eliminating 0 rows in a matrix with other rows having 0s and 1s

averagenon-zerosumzero elements

Hi guys.
So I have this matrix NxM size. Each row represents a spiking pattern of a neuron in one time frame. Each row which has 0s or 1s, i.e. spikes, is followed by 2 rows with all 0s.
I want to find the average firing rate per x milliseconds time bin, which is, say Y points in the 2nd dimension. Now if I run a loop like this –
avg_rate = [];
for i = 1:bin:size(spikes_21)-bin
avg1 = mean(spikes_21(:,i:i+bin-1),1);
avg_rate = [avg_rate avg1];
end
I will end up with a much lower average because even the 0s will be added up and averaged.
I could count the number of rows with non-zero elements, sum up each bin and divide by the number of valid rows. But that'll take another loop and I'm already processing 32 channels and each channel takes 40 mins to run, and then this process is to be done for the 21st channel. So it'll be extremely long.
Is there a simpler way to do this?
Thanks.

Best Answer

If you're trying to eliminate all the zero-on;ly rows, try something like this:
a =
0.7577 0.8235 0.4898 0.4984 0.9593
0 0.6948 0.4456 0.9597 0.5472
0 0 0 0 0
0.6555 0.9502 0.7094 0.5853 0.1493
0.1712 0.0344 0.7547 0.2238 0.2575
0 0 0 0 0
0 0 0 0 0
0.2769 0.7655 0.6551 0.5060 0.8143
0.0462 0.7952 0.1626 0.6991 0.2435
0.0971 0.1869 0.1190 0.8909 0.9293
Then use the following line of code to remove those rows:
a(~any(a,2),:) = []