2010-02-19 77 views
3

我有一个大阵列15x15x2200。这仅仅是15x15稀疏矩阵的集合,描绘了15个节点之间的链接以及它们如何在2200个时间单位上变化。我需要计算每个链接持续多长时间。通过这个我的意思是,假设A [4,11]直到时间单元5为0,并且保持1直到时间单元20,然后变成0并且再次从42变成46,我想将这些信息排列成数组中存储这些长度单独像LEN = {... 15,4,....}计算数组元素持久性算法

我想在matlab中做这个,然后生成一个直方图。做这个的最好方式是什么?

回答

4

让我们尝试没有循环做到这一点。

%# 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) 

编辑:

要查看曲目的持久性的另一种视觉表现,称之为

figure,imshow(array2d) 

这说明白色条纹,无论你有一个链接序列,它会向您展示整体模式。

+0

@Jonas:我将代码格式化了一下,使其更易于阅读:) – Amro 2010-02-19 15:21:37

+0

@Amro:对不起,我在编辑时必须提交我的编辑。你介意再试一次吗? – Jonas 2010-02-19 15:22:27

+0

哎呀..好吧,让我们再试一次! – Amro 2010-02-19 15:23:06