필터 지우기
필터 지우기

How to subtracts elements in a list

조회 수: 2 (최근 30일)
Rishi Kiran Shankar
Rishi Kiran Shankar 2019년 6월 28일
댓글: Star Strider 2019년 7월 3일
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

채택된 답변

Star Strider
Star Strider 2019년 6월 28일
편집: Star Strider 2019년 6월 28일
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
Rishi Kiran Shankar
Rishi Kiran Shankar 2019년 7월 3일
Hi Star Strider. Thanks a lot!
Star Strider
Star Strider 2019년 7월 3일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2019년 6월 28일

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by