how do I add the maximum value of arr into arrSorted within the while loop?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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);
disp(ar1Down);
disp(sortedUp);
disp(SortedDown);
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
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)
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
arr = []; % empty array
if arr > 0
disp('all positive')
else
disp('not all positive')
end
更多回答(1 个)
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);
disp(ar1Down);
disp(sortedUp);
disp(SortedDown);
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
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!