average distance between column values in a matrix
Afficher commentaires plus anciens
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
Réponses (2)
per isakson
le 3 Mai 2012
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 commentaires
Brandon
le 3 Mai 2012
Brandon
le 3 Mai 2012
per isakson
le 3 Mai 2012
The major problem is the blips around Y. You must distinguish between the name of the variable and the value of the variable.
Brandon
le 3 Mai 2012
Brandon
le 3 Mai 2012
per isakson
le 3 Mai 2012
If it returns anything you must be aware that Matlab works column wise.
Andrei Bobrov
le 3 Mai 2012
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));
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!