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

1 view (last 30 days)
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.
  2 Comments
Maurice Boueri
Maurice Boueri on 24 Aug 2011
Are you trying to eliminate all rows that have only zeros?
Example, if your matrix is 10x5, as below:
0.7577 0.8235 0.4898 0.4984 0.9593
0.7431 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
Are you trying to remove the 3rd, 6th and 7th rows?
If that's not your question, could you please provide a concrete example that helps us understand it better?
Thanks!

Sign in to comment.

Accepted Answer

Maurice Boueri
Maurice Boueri on 24 Aug 2011
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),:) = []

More Answers (2)

BF83
BF83 on 24 Aug 2011
Perhaps the sparse-command can help you out.

Fangjun Jiang
Fangjun Jiang on 24 Aug 2011
A few things:
  • size(spikes_21) returns two numbers, you might want to use size(..,1) or size(..,2) to be clar.
  • Your variable avg_rate is not pre-allocated. That slows down things a lot.
  • You can pre-process your data to remove all-zero rows like below.
a=[0 1 0 1; 0 0 0 0; 1 1 0 1;0 0 0 0]
a(all(~a,2),:)=[]
Another recommendation is to treat your data as a black-and-white image, there might be functions to help you.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!