poster
2019-12-10, 20:30
我有一个大型阵列15x15x2200。这仅仅是15x15稀疏矩阵的集合,描述了15个节点之间的链接以及它们在2200个时间单位内的变化情况。我需要计算每个链接持续多少时间。我的意思是,假设A [4,11]在时间单位5之前为0,在时间单位20之前保持为1,然后变为0,然后又从42变为46,再次变为1,我希望将此信息数组放入一个分别存储这些长度的数组,例如LEN = {... 15,4,....}
我正在尝试在matlab中执行此操作,然后生成直方图。做这个的最好方式是什么?
回答:
让我们尝试不使用循环。
%# 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) 无论您有多少个链接序列,它都将显示白色条纹,并且将向您显示总体图案。
更多&回答... (https://stackoverflow.com/questions/2296213)
我正在尝试在matlab中执行此操作,然后生成直方图。做这个的最好方式是什么?
回答:
让我们尝试不使用循环。
%# 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) 无论您有多少个链接序列,它都将显示白色条纹,并且将向您显示总体图案。
更多&回答... (https://stackoverflow.com/questions/2296213)