How to add each column of matrix A with another matrix B?

3 visualizaciones (últimos 30 días)
Hung Dao
Hung Dao el 14 de Abr. de 2021
Comentada: Hung Dao el 15 de Abr. de 2021
Suppose is a column vector and is a matrix, then , a matrix.
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
How should I do it in Matlab?
Thanks !

Respuesta aceptada

the cyclist
the cyclist el 14 de Abr. de 2021
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),[])
C = 2×6
6 7 8 25 26 27 11 12 13 48 49 50

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by