How to subtract only certain elements of a vector

11 Ansichten (letzte 30 Tage)
Stacy Genovese
Stacy Genovese am 5 Mai 2022
Beantwortet: John D'Errico am 5 Mai 2022
If I have two vectors of the same length, is it possible to subtract only certain elements?
a = [1 4 6 7; 3 4 5 6]
b = [4 2 7 8; 2 6 7 8]
normally I would do:
a(1,:) - b(1,:)
But how do I the same subtraction but only with elements 1,2 and 4?
Is this possible to do? Which element I want to skip will change each time through my loop.
Thanks

Akzeptierte Antwort

John D'Errico
John D'Errico am 5 Mai 2022
a and b are not vectors in your question, they are arrays.
However, if you want to do this, nothing stops you from selecting the elements you wish to subtract, and do so.
a = [1 4 6 7; 3 4 5 6];
b = [4 2 7 8; 2 6 7 8];
ind = [1 2 4];
c = a(1,ind) - b(1,ind);
c
c = 1×3
-3 2 -1

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by