Returning largest and smallest values in the array
18 views (last 30 days)
Show older comments
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 Comments
Jan
on 14 Sep 2011
@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
on 14 Sep 2011
@Jack: Please mention this page as source of ideas, if you submit your homework. Remember that teachers participate in this forum also.
Answers (7)
Grzegorz Knor
on 14 Sep 2011
See documentation:
doc min
doc max
See also:
1 Comment
Jan
on 14 Sep 2011
@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
on 14 Sep 2011
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 Comment
Jan
on 14 Sep 2011
+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
on 14 Sep 2011
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 Comments
Andrei Bobrov
on 14 Sep 2011
Hi Jan!
Small correct:
[value, index] = sort(array(:),'descend');
or
S = value(1);
Sidx = index(1);
L = value(end);
Lidx = index(end);
Jan
on 14 Sep 2011
@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
on 14 Sep 2011
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 Comment
Jan
on 14 Sep 2011
@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
on 14 Sep 2011
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 Comments
Sean de Wolski
on 14 Sep 2011
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
on 14 Sep 2011
@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 .
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!