我有两个大于1000行的矩阵和两列。每次第一列是“0”,第二列有一个值,每次第一列是“1”,第二列是零。示例:
M = [0 23;0 35;1 0;1 0;0 2;1 0]
M =
0 23
0 35
1 0
1 0
0 2
1 0让我们把第二列作为一个非周期周期来考虑。
我想要的是,每次第一列是0(直到再次是一列),有机会分析第二列的大小和总和。最后,我想知道哪个周期在大小和总和上是更大的。(在示例矩阵中,作为输出,我知道第一个周期是较大的,之和为58)。
发布于 2015-12-17 18:10:09
假设A是输入的两列数组,下面是一种使用accumarray -
%// Create ID array for using with accumarray
id = cumsum([1;diff(A(:,1))~=0]);
%// Get summations and counts for all IDs
sums = accumarray(id,A(:,2));
counts = accumarray(id,1);
%// Get offset in case the starting element in first column is not 0
offset = (A(1,1)~=0)+1;
%// Consider only even IDs corresponding to 0 elements cycle
sums = sums(offset:2:end)
counts = counts(offset:2:end)样本运行-
A =
1 34
1 45
0 23
0 35
1 0
1 0
0 2
0 8
0 6
1 9
sums =
58
16
counts =
2
3https://stackoverflow.com/questions/34341203
复制相似问题