views:

72

answers:

1

I have a large array 15x15x2200. THis is merely a collection of 15x15 sparse matrices depicting the links between 15 nodes and how they vary over 2200 time units. I need to calculate for how much time does each link persist. By this I mean, suppose A[4,11] is 0 till time unit 5 and stays 1 till time unit 20 and then becomes 0 and again becomes 1 from 42 to 46, I would like to have an array of this information into an array which stores these lengths separately like LEN = {...15, 4, ....}

I am trying to do this in matlab and then generate a histogram. What is the best way to do this?

+4  A: 

Let's try to do this without loops.

%# random adjacency matrix
array = randi([0 1], [15 15 2200]);

%# get the size of the array
[n1,n2,n3] = size(array);

%# reshape it so that it becomes n3 by n1*n2
array2d = reshape(array,[],n3)';

%# make sure that every run has a beginning and an end by padding 0's
array2d = [zeros(1,n1*n2);array2d;zeros(1,n1*n2)];

%# take the difference. +1 indicates a start, -1 indicates an end
arrayDiff = diff(array2d,1,1);
[startIdx,startCol] = find(arrayDiff==1);
[endIdx,endCol] = find(arrayDiff==-1);

%# since every sequence has a start and an end, and since find searches down the columns
%# every start is matched with the corresponding end. Simply take the difference
persistence = endIdx-startIdx; %# you may have to add 1, if 42 to 46 is 5, not 4

%# plot a histogram - make sure you play with the number of bins a bit
nBins = 20;
figure,hist(persistence,nBins)

Edit:

To see another visual representation of the persistence of your tracks, call

figure,imshow(array2d)

This shows white stripes wherever you have a sequence of links, and it will show you overall patterns.

Jonas
@Jonas: I formatted the code a bit to make it easier to read :)
Amro
@Amro: sorry, I must have submitted my edit while you were editing. Would you mind trying again, please?
Jonas
oops.. ok lets try that again!
Amro
Thanks! Also, good idea to add the random array!
Jonas
@Jonas : Thanks. This is similar to the code which I wrote. I was just wondering if there is some way which was even faster.
Ankur Chauhan