Effacer les filtres
Effacer les filtres

Adding Value in the array with proper indexing

3 vues (au cours des 30 derniers jours)
Maheen Fazal
Maheen Fazal le 27 Mar 2019
Commenté : Maheen Fazal le 29 Mar 2019
Hi, here i have an array with random values, i have sort them and divide them in maximum and minimum values, after sorting the array the index of values is changes, if i want to add the minvalues in the orignal unsorted array with proper indexing. how should i do it?
ME=4;
E=rand(1,ME)
ES=sort(E,'descend')
Ls=length(ES);
Hl=(Ls/2);
maxval=ES(1:Hl);
minval=ES(Hl+1:end);
  5 commentaires
Rik
Rik le 27 Mar 2019
That is just the output of your current code, not the output of want as the end result. What vector do you want to have?
Maheen Fazal
Maheen Fazal le 27 Mar 2019
I want to add
E(4)+minval(1) and
E(1)+minval(2)

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 28 Mar 2019
ME=4;
E=rand(1,ME);
[ES,ii]=sort(E,'descend');
Ls=length(ES);
Hl=(Ls/2);
maxval=ES(1:Hl);
mxii = ii(1:Hl);
minval=ES(Hl+1:end);
mnii = ii(Hl+1:end);
Enew = E;
Enew(mnii) = Enew(mnii) + minval

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 27 Mar 2019
ME=4;
E=rand(1,ME);
[ES,ii]=sort(E,'descend');
Ls=length(ES);
Hl=(Ls/2);
maxval=ES(1:Hl);
mxii = ii(1:Hl);
minval=ES(Hl+1:end);
mnii = ii(Hl+1:end);
Enow = sortrows([maxval(:),mxii(:);minval(:),mnii(:)],2);
Enow = Enow(:,1);
  1 commentaire
Maheen Fazal
Maheen Fazal le 28 Mar 2019
Sir, i want to add minval in the E array with same index values
e.g. if in the minval i have index 3,1 so i want to add them in the E array in their corresponding positions

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by