subtract each row from from matrix A from all rows matrix b
1 次查看(过去 30 天)
显示 更早的评论
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]
0 个评论
回答(1 个)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!