Filter löschen
Filter löschen

Sort elements of Array by given condition

4 Ansichten (letzte 30 Tage)
Daniel
Daniel am 28 Dez. 2022
Bearbeitet: Matt J am 28 Dez. 2022
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 Kommentare
Daniel
Daniel am 28 Dez. 2022
There is the data. Thanks a lot!
Torsten
Torsten am 28 Dez. 2022
Bearbeitet: Torsten am 28 Dez. 2022
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 28 Dez. 2022
Bearbeitet: Matt J am 28 Dez. 2022
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

Weitere Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 28 Dez. 2022
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

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by