필터 지우기
필터 지우기

How do I subtract every row of a matrix with every row of a second matrix?

조회 수: 20 (최근 30일)
Paola
Paola 2021년 10월 25일
댓글: Paola 2021년 10월 26일
Hi,
I have two matrices A (26 rows X 5100 columns) and B (26rows X 5100 columns) and I want to subtract every row of A with every row of B to obtain another matric C (2601 rows X 5100 columns). How can I have the matrix C?

채택된 답변

ChrisR
ChrisR 2021년 10월 25일
Here's how I understood the question. If you have A = magic(3) and B = eye(3)--that is,
A = [8 1 6; 3 5 7; 4 9 2];
B = [1 0 0; 0 1 0; 0 0 1];
then you can subtract the rows of B from each row of A with
m = size(A,1);
C = repelem(A,m,1) - repmat(B,m,1);
This approach gives
C = [7 1 6; 8 0 6; 8 1 5; 2 5 7; 3 4 7; 3 5 6; 3 9 2; 4 8 2; 4 9 1];
But in the example given in the question, this approach would produce a 676x5100 matrix, not a 2601x5100 matrix.
  댓글 수: 1
Paola
Paola 2021년 10월 26일
Thank you vary much Chris,
it worked perfectly. And yes sorry my matrices were 51X5100 not 26X5100.
Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by