Effacer les filtres
Effacer les filtres

How to set the minimum value of an array to a negative number?

4 vues (au cours des 30 derniers jours)
Kristen O'Mara
Kristen O'Mara le 29 Mar 2018
Commenté : Star Strider le 29 Mar 2018
I made a 1x50 random array and I need to find the max and set it equal to 100 and find the min and set equal to -100. Then I need to sort it and make two new arrays using indexes 1 - 25 and 26 - 50. I'm having trouble setting the values for the min/max. Here's what I have:
randArray50 = rand(1, 50);
min(randArray50) = -100;
max(randArray50) = 100;
sort(randArray50, 'ascend');
newArray1 = randArray50(1, 25);
newArray2 = randArray50(26, 50);
end
I'm getting an error saying
Subscript indices must either be real positive integers or logicals.
in the line using min.

Réponses (1)

Star Strider
Star Strider le 29 Mar 2018
The easiest way is to ask for two outputs from the max and min functions. The second output is the index of the first occurrence of each. Use that to index into the vector.
randArray50 = rand(1, 50);
[minv,minidx] = min(randArray50); % Mininum Value & Index Of First Occurrence
[maxv,maxidx] = max(randArray50); % Maxinum Value & Index Of First Occurrence
randArray50(minidx) = -100;
randArray50(maxidx) = 100;
You do the rest.
  2 commentaires
Kristen O'Mara
Kristen O'Mara le 29 Mar 2018
Thank you!
Star Strider
Star Strider le 29 Mar 2018
My pleasure!

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