필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Kristen O'Mara
Kristen O'Mara 2018년 3월 29일
댓글: Star Strider 2018년 3월 29일
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.

답변 (1개)

Star Strider
Star Strider 2018년 3월 29일
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.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by