How to add each column of matrix A with another matrix B?
조회 수: 2 (최근 30일)
이전 댓글 표시
Suppose
is a
column vector and
is a
matrix, then
, a
matrix.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583481/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583486/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583491/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583496/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583501/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583506/image.png)
Now, I have matrix
, each
is a
column vector. I would like to add each column in A, starting from the first column
, with matrix B and store the result matrix C. The resulting matrix C now has dimension
and looks like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583511/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583516/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583521/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583526/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583531/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/583536/image.png)
How should I do it in Matlab?
Thanks !
댓글 수: 0
채택된 답변
the cyclist
2021년 4월 14일
I think this does what you want:
% Input data
A = [1 20;
3 40];
B = [5 6 7;
8 9 10];
% Algorithm:
% (1) Permute A, transforming it from MxN matrix to Mx1xN.
% (2) Add it to B, using implicit expansion so that all columns are added.
% (3) Reshape so that slices in 3rd dimension are now side-by-side instead.
C = reshape(permute(A,[1 3 2]) + B, size(B,1),[])
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!