Sort elements of Array by given condition

Hello,
i have a vector n 480x1 and would like to sort the vector so that the condition
sum(([n ;0]-[0 ;n])>0)==68
is given. How could i do this? my approach was to realize a permutation of the vector until the condition is given. Unfortnely, this takes too much time.
Thanks in advance

댓글 수: 5

Dyuman Joshi
Dyuman Joshi 2022년 12월 28일
편집: Dyuman Joshi 2022년 12월 28일
That will depend upon the values of the vector.
You might not achieve that condition if all the values are monotonically increasing or decreasing.
Or do you want to change the indices of the values (jumble them up) to get this specific condition?
Daniel
Daniel 2022년 12월 28일
Yes exactly! i want to change the indices of the values in the vector so that the condition ist true
Dyuman Joshi
Dyuman Joshi 2022년 12월 28일
Please attach your data using the paperclip button.
Although, I must say that it might be a challenging task to do so.
Daniel
Daniel 2022년 12월 28일
There is the data. Thanks a lot!
Torsten
Torsten 2022년 12월 28일
편집: Torsten 2022년 12월 28일
For that there are 68 pairs such that
n(i+1) - n(i) > 0, n(0) = 0 (i=0,...,67)
your array must have at least 68 different elements.
Yours has only 27.

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

 채택된 답변

Matt J
Matt J 2022년 12월 28일
편집: Matt J 2022년 12월 28일
n=load('vector').xr;
m=numel(n);
n=n(randperm(m));
loc=find(([n ;0]-[0 ;n])>0);
n(loc(68):end)=sort(n(loc(68):end),'descend');
sum(([n ;0]-[0 ;n])>0)
ans = 68

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 12월 28일
load('vector.mat')
XR1 = [xr;0]; XR2 = [0;xr];
IND = (XR1-XR2)>0;
ii = 1;
S = 0;
while S~=68
S(ii+1) = S(ii)+IND(ii);
ii=ii+1;
end
fprintf('Summation is halted at %d step and the sum is %d \n', ii, S(ii))
Summation is halted at 226 step and the sum is 68

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2022년 12월 28일

편집:

2022년 12월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by