Divide a matrix with an array along a specific dimension
조회 수: 13 (최근 30일)
이전 댓글 표시
A=rand(10,20,30);
B=[1:30];
I want to divide A with B along the third dimension. I got the required result using loop.
for ii=1:30
C(:,:,ii)=A(:,:,ii)/B(ii);
end
But is there any pre-defined function which does the same operation, which takes the dimension as a input along which we want to divide a matrix.
댓글 수: 0
채택된 답변
Stephen23
2018년 3월 16일
편집: Stephen23
2018년 3월 16일
Once you make the sizes compatible you can do this using standard methods without any loop. Use permute or reshape to get the correct size of B. For example In R2016b and later a standard rdivide will do this:
A ./ reshape(B,1,1,[])
bsxfun(@rdivide,A,reshape(B,1,1,[]))
추가 답변 (1개)
Akira Agata
2018년 3월 16일
How about using arrayfun ?
C = arrayfun(@(k) A(:,:,k)./B(k), 1:30,'UniformOutput',false);
C = cat(3,C{:});
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!