2010-02-19 42 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