Borrar filtros
Borrar filtros

How to subtracts elements in a list

2 visualizaciones (últimos 30 días)
Rishi Kiran Shankar
Rishi Kiran Shankar el 28 de Jun. de 2019
Comentada: Star Strider el 3 de Jul. de 2019
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

Respuesta aceptada

Star Strider
Star Strider el 28 de Jun. de 2019
Editada: Star Strider el 28 de Jun. de 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 comentarios
Rishi Kiran Shankar
Rishi Kiran Shankar el 3 de Jul. de 2019
Hi Star Strider. Thanks a lot!
Star Strider
Star Strider el 3 de Jul. de 2019
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 28 de Jun. de 2019

Categorías

Más información sobre Elementary Math en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by