Sum a value in a matrix
Show older comments
Hi!
How could I sum a value in a row of a Matrix? Imagine I have:
(0,0,0;0,0,0;0,0,0;0,0,0) and I would like to have (0,0,0;3,3,3;0,0,0;0,0,0)
How could I get this result?
Thank you
Accepted Answer
More Answers (1)
Ameer Hamza
on 7 Oct 2020
If you want to sum it to an arbitrary matrix row, then you can try the following methods
M = [1 2 3; 4 5 6; 7 8 9; 10 11; 12];
M_new = M + [0; 3; 0; 0];
This will add 3 to the second row of M. Similarly; you can also use the following equivalent statement
M_new = M;
M_new(2,:) = M_new(2,:)+3;
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!