Returning largest and smallest values in the array
조회 수: 18 (최근 30일)
이전 댓글 표시
Hi everyone I am trying to create a function that returns the largest and smallest values in an array as well as the indicies of the largest and smallest value. I know what I need to do however I cannot put this as a function. The function should be
function [L, S, Lidx, Sidx] = largest_and_smallest(array)
%LARGEST_AND_SMALLEST Largest and smallest values in array
% [L, S, Lidx, Sidx] = LARGEST_AND_SMALLEST(ARRAY) returns
% the largest and smallest values in ARRAY, as well as the
% indices of the largest and smallest values.
% An error is raised if ARRAY is empty.
An example of this would be
>> [L,S,Lidx,Sidx] = largest_and_smallest([1,2,3,-400,5,6,700,8,9])
L =
700
S =
-400
Lidx =
7
Sidx =
4
댓글 수: 4
Jan
2011년 9월 14일
@Binkal: Please post an answer in the answer section, not as comment.
The comparisons "array(i)>array(i-1)" waste time and can be omitted.
Jan
2011년 9월 14일
@Jack: Please mention this page as source of ideas, if you submit your homework. Remember that teachers participate in this forum also.
답변 (7개)
Grzegorz Knor
2011년 9월 14일
See documentation:
doc min
doc max
See also:
댓글 수: 1
Jan
2011년 9월 14일
@Grzegorz: This is actually the optimal solution. Jack added the restriction, that MIN and MAX are not allowed, which is typical of stupid homework questions. But such limitations mean, that the pupils are forced to use MATLAB in a most inefficient way. A good programming homework would do the opposite! Therefore I suggest to reject the limitation and submit a solution using MIN and MAX. Basta.
Andrei Bobrov
2011년 9월 14일
function varargout = largest_and_smallest(A)
% call largest_and_smallest.m: [L,S,Lidx,Sidx] = largest_and_smallest(A)
[c idx] = cellfun(@(x)x(A(:)),{@max @min},'un',0);
n =size(A,1);
varargout = [c cellfun(@(x)[rem(x-1,n)+1, ceil(x/n)],idx,'un',0)];
ADD 14:35 MDT [06:35EDT]
variant without :) max and min
function [L, S, Lidx, Sidx] = largest_and_smallest(array)
A = array(:);
N = sum(bsxfun(@ge,A,A'));
Lidx = find(N == numel(A));
Sidx = find(N == 1);
L = A(Lidx);
S = A(Sidx);
댓글 수: 1
Jan
2011년 9월 14일
+1: SUM(BSXFUN(@ge)) is definitely neither MIN nor MAX.
I seems like we make a fair solution of the homework nearly impossible, because it is getting harder and harder to let the OP find an own solution...
Jan
2011년 9월 14일
function [L, S, Lidx, Sidx] = largest_and_smallest(array)
if isempty(array)
error('Input array is empty');
end
[value, index] = sort(array(:));
S = value(1);
Sidx = index(1);
L = value(end);
Lidx = index(end);
[EDITED]: Swapped S and L - thanks Andrei.
댓글 수: 2
Andrei Bobrov
2011년 9월 14일
Hi Jan!
Small correct:
[value, index] = sort(array(:),'descend');
or
S = value(1);
Sidx = index(1);
L = value(end);
Lidx = index(end);
Jan
2011년 9월 14일
@Andrei: Correct. I've written this using 'H' and 'L' for high and low. Then the final conversion to the user's style failed.
TAB
2011년 9월 14일
function [L, S, Lidx, Sidx] = largest_and_smallest(array)
L=array(1);
S=array(1);
Lidx=1;
Sidx=1;
for i=2:length(array)
if(array(i)<S)
S=array(i);
Sidx=i;
end
end
for i=2:length(array)
if(array(i)>L)
L=array(i);
Lidx=i;
end
end
end
댓글 수: 1
Jan
2011년 9월 14일
@Tabrez: Running the loop twice wastes time. If you join the loops one comparison would be enough, because a new value cannot be a new minimum and maximum at the same time:
for i=2:length(array)
if array(i) < S, S = array(i); Sidx = i;
elseif array(i) > L, L = array(i); Lidx = i; end
end
Jan
2011년 9월 14일
Homework questions, which forbid the usage of efficient built-in MATLAB functions, are stupid. It would be much more helpful to learn, how MATLAB can be used as efficient as possible.
Fortunately there is an efficient method to solve the problem without MIN and MAX: Get a bunch of spaghetti, cut them in a length proportional to the values of the array and write the index on each noodle. Then lift the bunch by a hand or forklift truck and push it against a wall. Afterwards finding the shortest and longest noodle is easy.
For large arrays this sorting method is surprisingly fast and O(n).
댓글 수: 2
Sean de Wolski
2011년 9월 14일
I thought writing my first bubble sort program and find a string in a second string were both very useful in my (non-computer-science) programming education.
Jan
2011년 9월 14일
@Sean: I agree. Of course I did not obtain my knowledge about sorting algorithms from calling FIND also. And my MinMaxElem submission mentioned above is meaningful only, because it does *not* use the built-in MIN and MAX.
Therefore I think a homework should include both branches: "Create two implementations, one with and one without built-in functions, and compare the run-time and memory consumptions".
My professor for "computing of scientific functions" told me not to trust any built-in functions, even if they are implemented in the processor. Funny, isn't it? But then I've learned it the hard way by failing over the bad implementation of LOG10 in Matlab, see Kahan's artical http://www.cs.berkeley.edu/~wkahan/LOG10HAF.TXT .
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!