How to quary (building a new matrix based on some information)

1 次查看(过去 30 天)
Matrix A & B are as follow. I want to create matrix C, where it includes only ID(s) available from A in B.
A = [
145654
145834
];
B = [
145654 1 1 5 1
145654 1 1 5 1
145654 1 1 5 1
145654 2 1 5 1
145654 2 1 5 1
145654 3 1 5 1
145654 3 1 5 1
145654 4 1 5 1
145654 9 1 5 1
145654 10 1 5 1
145654 10 1 5 1
145655 1 1 5 1
145655 1 1 5 1
145655 1 1 5 1
145655 1 1 5 1
145655 2 1 5 1
145655 3 1 5 1
145655 3 1 5 1
145655 4 1 5 1
145655 4 1 5 1
145655 4 1 5 1
145655 5 1 5 1
145655 7 1 5 1
145655 8 1 5 1
145655 9 1 5 1
145655 9 1 5 1
145655 10 1 5 1
145655 10 1 5 1
145655 10 1 5 1
145655 10 1 5 1
145655 10 1 5 1
145655 10 1 5 1
145834 1 1 1 6
145834 1 1 1 6
145834 2 1 1 6
145834 2 1 1 6
145834 3 1 1 6
145834 3 1 1 6
145834 4 1 1 6
145834 9 1 1 6
145834 10 1 1 6
145834 10 1 1 6
145834 10 1 1 6
145834 10 1 1 6
];
C = [
145654 1 1 5 1
145654 1 1 5 1
145654 1 1 5 1
145654 2 1 5 1
145654 2 1 5 1
145654 3 1 5 1
145654 3 1 5 1
145654 4 1 5 1
145654 9 1 5 1
145654 10 1 5 1
145654 10 1 5 1
145834 1 1 1 6
145834 1 1 1 6
145834 2 1 1 6
145834 2 1 1 6
145834 3 1 1 6
145834 3 1 1 6
145834 4 1 1 6
145834 9 1 1 6
145834 10 1 1 6
145834 10 1 1 6
145834 10 1 1 6
145834 10 1 1 6
];
And finally, I want to create matrix D, where arrays in first column of matrix C are equal only to 1:
D = [
145654 1 1 5 1
145654 1 1 5 1
145654 1 1 5 1
145834 1 1 1 6
];

回答(1 个)

James Tursa
James Tursa 2017-5-4
C = B(ismember(B(:,1),A(:,1)),:);
D = C(C(:,2)==1,:);

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by