subtract each row from from matrix A from all rows matrix b

조회 수: 1 (최근 30일)
Matthew
Matthew 2015년 10월 27일
댓글: Matthew 2015년 10월 27일
I have these 2 matrices. I am looking to subtract each row in B for all rows in A. The result put into a C.
A = [1 2 3;4 5 6;7 8 9]; B = [1 1 1;2 2 2];
the result should look like this: C(:,:,1)= [0 1 2;3 4 5;6 7 8] C(:,:,2)= [-2 -1 0;1 2 3;4 5 6]

답변 (1개)

Star Strider
Star Strider 2015년 10월 27일
I don’t understand how you got C(:,:,2). This otherwise seems to work:
A = [1 2 3;4 5 6;7 8 9];
B = [1 1 1;2 2 2];
C = cat(3,bsxfun(@minus, A, B(1,:)), bsxfun(@minus, A, B(2,:)))
C(:,:,1) =
0 1 2
3 4 5
6 7 8
C(:,:,2) =
-1 0 1
2 3 4
5 6 7
  댓글 수: 1
Matthew
Matthew 2015년 10월 27일
That's great, thank you!! But it is not quite what i need. In reality, I have an A and B matrix that has 1000's of coordinates. I would like to do this calculation in a single line for all. Is that possible. My attempts to modify your line have been unsuccessful thus far. ANy thoughts?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by