필터 지우기
필터 지우기

how do I add the maximum value of arr into arrSorted within the while loop?

조회 수: 1 (최근 30일)
John
John 2023년 11월 11일
편집: Voss 2023년 11월 11일
clear; clc
rng(1)
a1D = randi([-20,80], 1, randi(20));
a2D = randi([-50, 30], randi(10), randi(16));
ar1DUp = Sort1DAr(a1D, "up");
ar1Down = Sort1DAr(a1D, "down");
sortedUp = Sort1DAr(a2D, "up");
SortedDown = Sort1DAr(a2D, 'down');
disp(ar1DUp);
disp(ar1Down);
disp(sortedUp);
disp(SortedDown);
function arrSorted = Sort1DAr(arr, sortOrder)
arrSorted = [];
while arr > 0
[M, I] = max(arr(:));
arrSorted = [arrSorted, M];
arr(I) = [];
end
if sortOrder == "up"
fliplr(arrSorted);
end
end

채택된 답변

Voss
Voss 2023년 11월 11일
편집: Voss 2023년 11월 11일
while arr > 0
This iterates while all elements of the array arr are greater than zero. If arr contains elements that are less than or equal to zero (which is the case for these a1D and a2D created after rng(1)), the loop iterates zero times. That is, It is never true (for these a1D and a2D) that all elements of arr are greater than zero.
I think what you intended was to iterate as long as arr has some elements. For that you can use
while ~isempty(arr)
Another problem is that when sortOrder is "up" you need to assign the result from fliplr() to arrSorted, which will be returned from the function.
rng(1)
a1D = randi([-20,80], 1, randi(20));
a2D = randi([-50,30], randi(10), randi(16));
ar1DUp = Sort1DAr(a1D, "up");
ar1Down = Sort1DAr(a1D, "down");
sortedUp = Sort1DAr(a2D, "up");
SortedDown = Sort1DAr(a2D, 'down');
disp(ar1DUp);
-20 -11 -6 -2 10 14 20 34 52
disp(ar1Down);
52 34 20 14 10 -2 -6 -11 -20
disp(sortedUp);
Columns 1 through 33 -49 -49 -48 -47 -46 -46 -44 -43 -42 -42 -40 -39 -39 -37 -34 -34 -33 -29 -28 -27 -27 -25 -25 -17 -17 -17 -16 -14 -11 -7 -7 -5 -4 Columns 34 through 55 -3 3 4 4 5 6 6 6 6 10 10 13 14 17 20 21 21 22 23 27 28 30
disp(SortedDown);
Columns 1 through 33 30 28 27 23 22 21 21 20 17 14 13 10 10 6 6 6 6 5 4 4 3 -3 -4 -5 -7 -7 -11 -14 -16 -17 -17 -17 -25 Columns 34 through 55 -25 -27 -27 -28 -29 -33 -34 -34 -37 -39 -39 -40 -42 -42 -43 -44 -46 -46 -47 -48 -49 -49
function arrSorted = Sort1DAr(arr, sortOrder)
arrSorted = [];
while ~isempty(arr) % Iterate as long as there are some elements remaining in arr
[M, I] = max(arr(:)); % Get the value and index of the maximum element
arrSorted = [arrSorted, M]; % Append the maximum element to the sorted vector
arr(I) = []; % Remove the maximum element from arr
end
if sortOrder == "up"
% need to assign the result of fliplr to arrSorted, so that the flipped
% vector is what is returned from this function
arrSorted = fliplr(arrSorted);
end
end
  댓글 수: 1
Voss
Voss 2023년 11월 11일
Note that if all elements of arr are in fact positive, then the loop as you wrote it works.
arr = 1:5;
arrSorted = [];
while arr > 0
[M, I] = max(arr(:)); % Get the value and index of the maximum element
arrSorted = [arrSorted, M]; % Append the maximum element to the sorted vector
arr(I) = []; % Remove the maximum element from arr
end
disp(arrSorted)
5 4 3 2 1
This is because, as elements are removed from arr, it remains true that all elements of arr are positive, until the point when arr has no elements, at which point arr > 0 evaluates to false.
arr = [1 2 3]; % some non-empty array of positive numbers
if arr > 0 % "if" works as good as "while" for this purpose
disp('all positive')
else
disp('not all positive')
end
all positive
arr = []; % empty array
if arr > 0
disp('all positive')
else
disp('not all positive')
end
not all positive

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 11월 11일
Here is how ti can be attained:
clearvars; clc
rng(1)
a1D = randi([-20,80], 1, randi(20));
a2D = randi([-50, 30], randi(10), randi(16));
ar1DUp = Sort1DAr(a1D, "up");
ar1Down = Sort1DAr(a1D, "down");
sortedUp = Sort1DAr(a2D, "up");
SortedDown = Sort1DAr(a2D, 'down');
disp(ar1DUp);
10 14 20 34 52
disp(ar1Down);
52 34 20 14 10
disp(sortedUp);
3 4 4 5 6 6 6 6 10 10 13 14 17 20 21 21 22 23 27 28 30
disp(SortedDown);
30 28 27 23 22 21 21 20 17 14 13 10 10 6 6 6 6 5 4 4 3
function arrSorted = Sort1DAr(arr, sortOrder)
arrSorted = [];
arr = arr(:);
arr = arr(arr>0);
N=numel(arr);
ii=0;
while ii<N+1
ii=ii+1;
[M, I] = max(arr);
arrSorted = [arrSorted, M];
arr(I) = [];
end
if sortOrder == "up"
arrSorted = fliplr(arrSorted);
else
arrSorted = arrSorted;
end
end

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by