Create a new matrix

1 次查看(过去 30 天)
Let A a matrix and B a vector defined by :
A=[ 4 2
7 4
3 5
6 8
8 1];
B= [4 6 7 8];
I wish to know how to create a new matrix C which is a part of matrix A but necessary contain an element of the vector B, given by
C = [ 7 4
6 8];
If anyone could help , I'd greaty appreciate it.
  2 个评论
Sam Chak
Sam Chak 2022-4-18
Sounds like an IQ puzzle.
What is the governing rule for the contruction of Matrix ?
A brief glance at Matrix shows that:
C = [A(2,:); A(4,:)]

请先登录,再进行评论。

采纳的回答

Bruno Luong
Bruno Luong 2022-4-18
A=[ 4 2
7 4
3 5
6 8
8 1];
B= [4 6 7 8];
C = A(all(ismember(A,B),2),:)
C = 2×2
7 4 6 8
  4 个评论
Bruno Luong
Bruno Luong 2022-5-12
编辑:Bruno Luong 2022-5-12
This code select the first two matched if there is more than 2 (like the last row of A):
A=[ 4 2 5
7 4 3
3 5 1
6 8 1];
B=[5 8 2 6 1];
At = A.';
tf = ismember(At,B);
tf = tf & cumsum(tf,1)<=2;
C = reshape(At(tf),2,[]).'
C = 3×2
2 5 5 1 6 8
Yamina chbak
Yamina chbak 2022-5-12
Yes, thanks you @Bruno Luong I understand this code. Thanks you again

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by