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

1 回表示 (過去 30 日間)
Abhilash
Abhilash 2011 年 8 月 24 日
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 件のコメント
Maurice Boueri
Maurice Boueri 2011 年 8 月 24 日
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!
Abhilash
Abhilash 2011 年 8 月 24 日
That's exactly what I'm trying to do!

サインインしてコメントする。

採用された回答

Maurice Boueri
Maurice Boueri 2011 年 8 月 24 日
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),:) = []
  1 件のコメント
Abhilash
Abhilash 2011 年 8 月 24 日
Thanks a million. That worked.

サインインしてコメントする。

その他の回答 (2 件)

BF83
BF83 2011 年 8 月 24 日
Perhaps the sparse-command can help you out.

Fangjun Jiang
Fangjun Jiang 2011 年 8 月 24 日
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.

カテゴリ

Help Center および File ExchangeTime Series Events についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by