average distance between column values in a matrix

I have a 16 row, 20,000 column matrix of 0's and 1's. 1's represent activity in each row, 0's represent a lack there of. I want to find the average distance (# of columns) between points of activity in the rows and then have an overall average of all 16 rows. So if, for example, a row had 01110001110, the distance would be 6, (columnar distance between centers of groups of random consecutive ones (or single ones). Any ideas?
Thank you

4 Comments

Perhaps you could start to explain, what you have tried so far.
I don't know where to begin on even these simple issues. I'm brand new to coding and learning as I go along.
I just get the pros input and then go read up on whatever functions you suggest (regexp, etc.)

Sign in to comment.

Answers (2)

See the answer, How to convert 0's and 1's to intergers, and then try:
cac = regexp( '000001111111111000000000011111', '(0+)|(1+)', 'match' );
num = cellfun( @numel, cac );
This is more than half way to a solution.
--- CONT. ---
Given that Y is a character array, .<16x20000 char>, try first with one row
cac = regexp( Y(1,:), '(0+)|(1+)', 'match');
if that works I guess the simplest (:KISS:) is to use a loop and operate on one row at a time.
--- CONT. ---
I put these lines in the editor and select the cell (yellow background) and click Evaluate cell ( or Cntrl+Enter)
cac = regexp( '000001111111111000000000011111', '(0+)|(1+)', 'match' );
num = cellfun( @numel, cac );
disp( num )
that gives me the following line in the command window
5 10 10 5
I have R2012a but that shouldn't matter. Why not try
Y = repmat( '11100011111111111110000000000011111111110000000', 6, 1 );
num = cell( 6, 1 );
for rr = 1 : 6
cac = regexp( Y(rr,:), '(0+)|(1+)', 'match');
num{rr} = cellfun( @numel, cac );
end
num{:}
that produces in the command window
ans =
3 3 13 11 10 7
ans =
3 3 13 11 10 7
ans =
3 3 13 11 10 7
ans =
3 3 13 11 10 7
ans =
3 3 13 11 10 7
ans =
3 3 13 11 10 7

6 Comments

So for my matrix (Y), i put cac=regexp('Y', '(0+)|(1+)', 'match');
This is returning a 0x0 cell, is that suggesting that within Y there are no 0's which turn into 1's in subsequent columns? ...Sorry for being so terrible at this coding stuff, I'm new!
The major problem is the blips around Y. You must distinguish between the name of the variable and the value of the variable.
I tried Y(1,:) already, it tells me "??? Undefined function or method 'regexp' for input arguments of type
'double'."
I tried copy and pasting the contents of the matrix into the regexp line, it gave me a 1x80,000 matrix. It didn't separate it out like with the '000001111111111000000000011111' example for some reason..
If it returns anything you must be aware that Matlab works column wise.

Sign in to comment.

use function regionprops from Image Processing Toolbox
x =[...
1 0 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1];
s = regionprops(x > 0,'Centroid' );
out0 = cat(1,s.Centroid);
out = diff(out0(:,1));
OR:
ii = find([true;diff(x(:))~=0]);
i2 = [ii,[ii(2:end)-1;numel(x)]];
out = diff(mean(i2(x(ii)>0,:),2));

Categories

Asked:

on 3 May 2012

Community Treasure Hunt

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

Start Hunting!