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,:)]
C = A([2 4],:)

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2022년 4월 18일

0 개 추천

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

Yamina chbak
Yamina chbak 2022년 4월 18일
Yees, It worked. Thanks!
Yamina chbak
Yamina chbak 2022년 5월 12일
Hi @Bruno Luong. I like your answer and I would like to learn from you. If you can help, I have an question about this problem but in the case of the matix A has 3 or 6 column , for example:
A=[ 4 2 5
7 4 3
3 5 1
6 8 1];
I wish to learn how to create a new matrix C which is a part of A and necessary to have two column and contain the elements of B:
B=[5 8 2 6 1]
to get like:
C = [2 5
5 1
6 8];
I will glad if you could help me.
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개)

카테고리

제품

릴리스

R2018b

질문:

2022년 4월 18일

댓글:

2022년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by