extracting an element in a vector

조회 수: 7 (최근 30일)
murti
murti 2017년 5월 3일
편집: Andrei Bobrov 2017년 5월 3일
there is a vector like A=[a b c d] and these a b c d are the numbers which I dont know the values. If I write max(A), then I find the max value element of this vector;and writing min(A), I can find the min element of this vector. but I want to create a new vector taking the two values of this A vector except these two max and min. I wanna take the other two element. so,how can i do this?
thanks so much.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 5월 3일
편집: Andrei Bobrov 2017년 5월 3일
[~,ii] = max(A);
[~,ii(2)] = min(A);
out = A(setdiff(1:numel(A),ii));
other way
[~,ii] = sort(A);
out = A(sort(ii(2:end-1)));
more way
out = A(A ~= max(A) & A ~= min(A));
  댓글 수: 2
murti
murti 2017년 5월 3일
it works, very much thanks.
Andrei Bobrov
Andrei Bobrov 2017년 5월 3일
편집: Andrei Bobrov 2017년 5월 3일
If we have repititions max's and min's that answers between 1st and 3th ways or 2st and 3th ways are different:
>> A = [100 5 8 5 100];
>> [~,ii] = max(A);
[~,ii(2)] = min(A);
out1 = A(setdiff(1:numel(A),ii))
out1 =
8 5 100
>> [~,ii] = sort(A);
out2 = A(sort(ii(2:end-1)))
out2 =
8 5 100
>> out3 = A(A ~= max(A) & A ~= min(A))
out3 =
8
>>

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by