Ho to subtract row from a row in a matrix?

5 views (last 30 days)
Atika
Atika on 10 Sep 2022
Answered: David Hill on 10 Sep 2022
I want to subtract first row of a matrix from a second row.
Considering the matrix to be P= [1 4.6 -7.6 2.2 1.4 6.8 18.2 12; 1 5.6 7.4 2.8 11.4 4.8 28.2 -13; 8 9.5 11.7 13.8 12.6 4.7 -8.8 -23]
please answer this by using a single command.
also explain your answer a bit for me to understand.
  1 Comment
Star Strider
Star Strider on 10 Sep 2022
With:
P= [1 4.6 -7.6 2.2 1.4 6.8 18.2 12; 1 5.6 7.4 2.8 11.4 4.8 28.2 -13; 8 9.5 11.7 13.8 12.6 4.7 -8.8 -23];
either
Out = [P(1,:)-P(2,:); P(3,:)]
Out = 2×8
0 -1.0000 -15.0000 -0.6000 -10.0000 2.0000 -10.0000 25.0000 8.0000 9.5000 11.7000 13.8000 12.6000 4.7000 -8.8000 -23.0000
or:
Out = diff(P)
Out = 2×8
0 1.0000 15.0000 0.6000 10.0000 -2.0000 10.0000 -25.0000 7.0000 3.9000 4.3000 11.0000 1.2000 -0.1000 -37.0000 -10.0000
.

Sign in to comment.

Answers (1)

David Hill
David Hill on 10 Sep 2022
P= [1 4.6 -7.6 2.2 1.4 6.8 18.2 12; 1 5.6 7.4 2.8 11.4 4.8 28.2 -13; 8 9.5 11.7 13.8 12.6 4.7 -8.8 -23];
p=P(2,:)-P(1,:)
p = 1×8
0 1.0000 15.0000 0.6000 10.0000 -2.0000 10.0000 -25.0000

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!