How do I sum a vector (row) to a matrix?

9 次查看(过去 30 天)
If I have
A=[1 0 0 1;
-1 0 1 1;
1 1 1 1]
B=[1 2 2 1]
How do I add the vector B to the 1st row of A so I get:
A=[2 2 2 3;
-1 0 1 1;
1 1 1 1]

采纳的回答

Star Strider
Star Strider 2021-11-16
This involves a bit of indexing.
Note that the rest of ‘A’ is unchnaged, and onlly ‘A(1,:)’ is involved in the operation.
A=[1 0 0 1;
-1 0 1 1;
1 1 1 1];
B=[1 2 2 1];
A(1,:) = A(1,:) + B
A = 3×4
2 2 2 2 -1 0 1 1 1 1 1 1
.

更多回答(1 个)

the cyclist
the cyclist 2021-11-16
编辑:the cyclist 2021-11-16
A=[1 0 0 1;
-1 0 1 1;
1 1 1 1];
B=[1 2 2 1];
A(1,:) = A(1,: ) + B
A = 3×4
2 2 2 2 -1 0 1 1 1 1 1 1

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by