Create a 3D Martix by operation of 1D vector on 2D matrix, using cat()

Given a 2D matrix A, and a 1D vector B, I would like to create a 3D Martix, with 2D slices of A minus each element of B.
So for a B of length 4, this works:
cat(3, A-B(1), A-B(2), A-B(3), A-B(4)))
The actual dimensions will be around A(40000,7), B(150), so I need to be able to do this for a given size of B

 채택된 답변

A = rand(10,5);
B = 1:3;
C = bsxfun(@minus, A, permute(B, [3 1 2]));
If B is a column vector (instead of a row vector), the permute would be
C = bsxfun(@minus, A, permute(B, [3 2 1]));

댓글 수: 1

Thanks, that's great, I tried bsxfun, but hadn't been using permute. This works perfectly.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2014년 7월 25일

댓글:

2014년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by