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
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
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
Grzegorz Knor 2011-9-14
  1 个评论
Jan
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
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
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
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
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
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
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
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
Jan 2011-9-14
A fast C-mex for this job: FEX: MinMaxElem

Jan
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
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
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 .

请先登录,再进行评论。


Greg
Greg 2011-9-16
Thanks for all the help everyone I agree it is stupid when this is the most inefficient way to do this and goes against everything in computational mathematics.

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by