Azzera filtri
Azzera filtri

How to subtracts elements in a list

2 visualizzazioni (ultimi 30 giorni)
I have list like;
A = [x1,x2,x3,x4];
i want a list which can subtract first element with all other element and so on.\
i need results like [x1-x2,x1-x3,x1-x4,x2-x3,x2-x4,x3-x4]
Any help would be appreciable.
Thanks in advance

Risposta accettata

Star Strider
Star Strider il 28 Giu 2019
Modificato: Star Strider il 28 Giu 2019
To get a vector output:
A = randi(9, 1, 4) % Create Vector
Dif = A(:) - A(:)'; % R2016b & Since
Dif = bsxfun(@minus, A(:), A(:)'); % R2016a & Previous
Out = (triu(Dif)+tril(nan(size(Dif))))';
Out = Out(~isnan(Out))'
so for example:
A =
8 9 7 2
Out =
-1 1 6 2 7 5
EDIT — Corrected comments. Code unchanged.
  5 Commenti
Rishi Kiran Shankar
Rishi Kiran Shankar il 3 Lug 2019
Hi Star Strider. Thanks a lot!
Star Strider
Star Strider il 3 Lug 2019
As always, my pleasure!

Accedi per commentare.

Più risposte (1)

Matt J
Matt J il 28 Giu 2019
A(:)-A(:).'

Categorie

Scopri di più su Elementary Math in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by