How to find a specific row in the matrix and extract it to the new matrix

39 ビュー (過去 30 日間)
Moe
Moe 2015 年 3 月 2 日
編集済み: Andrei Bobrov 2015 年 3 月 2 日
Can anyone give me any suggestion how I can find the specific row (matrix= criteria) from main matrix (big matrix) and then extract to the some sub-matrices (A, B, C)
Z = [1 1 1 2
2 2 3 1
3 1 3 4
4 2 1 3
5 1 1 2
6 2 3 1
7 1 3 4
8 1 1 2
9 2 3 1
10 1 3 4
11 1 1 2
12 2 1 3
13 2 1 3
14 2 1 3
15 2 3 1
16 1 3 4
17 2 1 3
18 1 1 2
19 1 1 2
20 2 3 1
21 2 3 1
22 1 3 4
23 1 1 2
24 2 1 3
25 2 1 3
26 2 3 1
27 1 3 4]
M = [1 1 2
2 3 1
1 3 4]
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 3 月 2 日
What is your criterion
Moe
Moe 2015 年 3 月 2 日
編集済み: Moe 2015 年 3 月 2 日
Criteria is the second matrix in the photo.
For example I need to find all of rows in the first matrix that according to the "A" criteria has [1 1 2]. Therefore, output should be same as third matrix:
[1 1 1 2
5 1 1 2
8 1 1 2
11 1 1 2
18 1 1 2
19 1 1 2
23 1 1 2]

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 3 月 2 日
編集済み: Andrei Bobrov 2015 年 3 月 2 日
Z = [1 1 1 2
2 2 3 1
3 1 3 4
4 2 1 3
5 1 1 2
6 2 3 1
7 1 3 4
8 1 1 2
9 2 3 1
10 1 3 4
11 1 1 2
12 2 1 3
13 2 1 3
14 2 1 3
15 2 3 1
16 1 3 4
17 2 1 3
18 1 1 2
19 1 1 2
20 2 3 1
21 2 3 1
22 1 3 4
23 1 1 2
24 2 1 3
25 2 1 3
26 2 3 1
27 1 3 4];
M = [1 1 2
2 3 1
1 3 4];
[l,i0] = ismember(Z(:,2:end),M,'rows');
zz = Z(l,:);
out = accumarray(i0(l),(1:nnz(i0))',[],@(x){sortrows(zz(x,:))});

その他の回答 (2 件)

Guillaume
Guillaume 2015 年 3 月 2 日
Use ismember with the 'rows' option:
ismatchrow = ismember(Z(:, 2:end), M(1, :), 'rows');
A = Z(ismatchrow, :)
Or to obtain a cell array with all the submatrices:
[~, matchrow] = ismember(Z(:, 2:end), M, 'rows');
matches = cell(1, max(matchrow));
for m = 1:max(matchrow)
matches{m} = Z(matchrow == m, :);
end
celldisp(matches)

Star Strider
Star Strider 2015 年 3 月 2 日
This works:
[Zu, ia, ic] = unique(Z(:,2:4), 'rows');
[Lia,Locb] = ismember(M,Zu,'rows');
for k1 = 1:size(M,1)
R{k1} = find(Locb(k1) == ic);
end
A = Z(R{1},:);
B = Z(R{2},:);
C = Z(R{3},:);

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by