Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
Kristen O'Mara
Kristen O'Mara am 29 Mär. 2018
Kommentiert: Star Strider am 29 Mär. 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.

Antworten (1)

Star Strider
Star Strider am 29 Mär. 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.

Kategorien

Mehr zu Matrix Indexing 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