Difference of elements of vector and matrix

조회 수: 1 (최근 30일)
Rafal Jaremski
Rafal Jaremski 2022년 3월 24일
댓글: Rafal Jaremski 2022년 3월 24일
Hello,
My input is A = [1, 2, 3, 4 ; 5, 6, 7, 8], B = [1, 3], my desired output is C = [1, 3 ; 5, 7].
Is it possible to use setdiff function here? I would rather not to use loop here because of the size of the data.

채택된 답변

Stephen23
Stephen23 2022년 3월 24일
A = [1,2,3,4;5,6,7,8]
A = 2×4
1 2 3 4 5 6 7 8
B = [1,3]
B = 1×2
1 3
[X,Y] = ismember(B(1,:),A(1,:));
C = A(:,Y(X))
C = 2×2
1 3 5 7
  댓글 수: 1
Rafal Jaremski
Rafal Jaremski 2022년 3월 24일
Thank You, this answer suits to my problem best!

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

추가 답변 (1개)

Davide Masiello
Davide Masiello 2022년 3월 24일
If the values of B are to be found strictly in the first row of A, then use this
clear,clc
A = [1, 2, 3, 4 ; 5, 6, 7, 8];
B = [1, 3];
C = A(:,any(A(1,:) == B',1))
C = 2×2
1 3 5 7

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by