以可变步长提取矩阵行
Extracting matrix rows with variable step
假设我有一个像这样的 3 列矩阵:
1 2 0,1 "A"
1 3 0,2 "B"
1 4 0,3 "C"
1 5 0,4
1 6 0,5
1 7 0,6
1 8 0,7
1 9 0,8
1 10 0,9
1 11 1
2 3 1,1 "A"
2 4 1,2 "B"
2 5 1,3 "C"
2 6 1,4
2 7 1,5
2 8 1,6
2 9 1,7
2 10 1,8
2 11 1,9
3 4 2 "A"
3 5 2,1 "B"
3 6 2,2 "C"
3 7 2,3
3 8 2,4
3 9 2,5
3 10 2,6
3 11 2,7
4 5 2,8 "A"
4 6 2,9 "B"
4 7 3 "C"
4 8 3,1
4 9 3,2
4 10 3,3
4 11 3,4
5 6 3,5 "A"
5 7 3,6 "B"
5 8 3,7 "C"
5 9 3,8
5 10 3,9
5 11 4
6 7 4,1 "A"
6 8 4,2 "B"
6 9 4,3 "C"
6 10 4,4
6 11 4,5
7 8 4,6 "A"
7 9 4,7 "B"
7 10 4,8 "C"
7 11 4,9
8 9 5 "A"
8 10 5,1 "B"
8 11 5,2 "C"
9 10 5,3 "A"
9 11 5,4 "B"
10 11 5,5 "A"
如何在不同的矩阵中提取所有标有 "A" 的行?步骤是 10, 9,..,2 对于标有 "B" 的行,然后用 "C" 等等,应该做同样的事情。输出应该是:
1 2 0,1
2 3 1,1 1 3 0,2
3 4 2 2 4 1,2
4 5 2,8 3 5 2,1
A = 5 6 3,5 4 6 2,9
6 7 4,1 B = 5 7 3,6
7 8 4,6 6 8 4,2
8 9 5 7 9 4,7
9 10 5,3 8 10 5,1
10 11 5,5 9 11 5,4
注意:3 列矩阵是 multcompare
的输出,其中前两列是 11 列矩阵之间的比较。
由于标有 A
的所有行在前两列中总是 x
和 x+1
,您可以使用:
A = Z(find(Z(:,1)==Z(:,2)-1),:)
B
相差二,因此,
B = Z(find(Z(:,1)==Z(:,2)-2),:)
等等,假设 Z
是你的初始数据。
假设 M
是输入数组,您可以在 A
的连续行 ID 与 [=21 之间使用衰减偏移量 [10 9 8 7 6 5 ...]
的模式=] 获取 M
中的实际行 ID。然后,使用 matrix-indexing
得到 M
的相应行,最终输出 A
、B
和 C
-
offsets = 10:-1:2 %// offsets between labels
ids = cumsum([1 offsets]) %// obtain actual row ids for A
A = M(ids,:) %// get the corresponding rows of M for A by directly using ids
%// Get the corresponding rows of M for B and C by using ids and adding 1 and 2
%// respectively as B and C are at offsets 1 and 2 with respect to A and
%// number of such row ids are one less for B and two less for C
B = M(ids(1:end-1)+1,:)
C = M(ids(1:end-2)+2,:)
假设我有一个像这样的 3 列矩阵:
1 2 0,1 "A"
1 3 0,2 "B"
1 4 0,3 "C"
1 5 0,4
1 6 0,5
1 7 0,6
1 8 0,7
1 9 0,8
1 10 0,9
1 11 1
2 3 1,1 "A"
2 4 1,2 "B"
2 5 1,3 "C"
2 6 1,4
2 7 1,5
2 8 1,6
2 9 1,7
2 10 1,8
2 11 1,9
3 4 2 "A"
3 5 2,1 "B"
3 6 2,2 "C"
3 7 2,3
3 8 2,4
3 9 2,5
3 10 2,6
3 11 2,7
4 5 2,8 "A"
4 6 2,9 "B"
4 7 3 "C"
4 8 3,1
4 9 3,2
4 10 3,3
4 11 3,4
5 6 3,5 "A"
5 7 3,6 "B"
5 8 3,7 "C"
5 9 3,8
5 10 3,9
5 11 4
6 7 4,1 "A"
6 8 4,2 "B"
6 9 4,3 "C"
6 10 4,4
6 11 4,5
7 8 4,6 "A"
7 9 4,7 "B"
7 10 4,8 "C"
7 11 4,9
8 9 5 "A"
8 10 5,1 "B"
8 11 5,2 "C"
9 10 5,3 "A"
9 11 5,4 "B"
10 11 5,5 "A"
如何在不同的矩阵中提取所有标有 "A" 的行?步骤是 10, 9,..,2 对于标有 "B" 的行,然后用 "C" 等等,应该做同样的事情。输出应该是:
1 2 0,1
2 3 1,1 1 3 0,2
3 4 2 2 4 1,2
4 5 2,8 3 5 2,1
A = 5 6 3,5 4 6 2,9
6 7 4,1 B = 5 7 3,6
7 8 4,6 6 8 4,2
8 9 5 7 9 4,7
9 10 5,3 8 10 5,1
10 11 5,5 9 11 5,4
注意:3 列矩阵是 multcompare
的输出,其中前两列是 11 列矩阵之间的比较。
由于标有 A
的所有行在前两列中总是 x
和 x+1
,您可以使用:
A = Z(find(Z(:,1)==Z(:,2)-1),:)
B
相差二,因此,
B = Z(find(Z(:,1)==Z(:,2)-2),:)
等等,假设 Z
是你的初始数据。
假设 M
是输入数组,您可以在 A
的连续行 ID 与 [=21 之间使用衰减偏移量 [10 9 8 7 6 5 ...]
的模式=] 获取 M
中的实际行 ID。然后,使用 matrix-indexing
得到 M
的相应行,最终输出 A
、B
和 C
-
offsets = 10:-1:2 %// offsets between labels
ids = cumsum([1 offsets]) %// obtain actual row ids for A
A = M(ids,:) %// get the corresponding rows of M for A by directly using ids
%// Get the corresponding rows of M for B and C by using ids and adding 1 and 2
%// respectively as B and C are at offsets 1 and 2 with respect to A and
%// number of such row ids are one less for B and two less for C
B = M(ids(1:end-1)+1,:)
C = M(ids(1:end-2)+2,:)