Returning largest and smallest values in the array

16 visualizaciones (últimos 30 días)
Greg
Greg el 14 de Sept. de 2011
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 comentarios
Jan
Jan el 14 de Sept. de 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
Jan el 14 de Sept. de 2011
@Jack: Please mention this page as source of ideas, if you submit your homework. Remember that teachers participate in this forum also.

Iniciar sesión para comentar.

Respuestas (7)

Grzegorz Knor
Grzegorz Knor el 14 de Sept. de 2011
  1 comentario
Jan
Jan el 14 de Sept. de 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.

Iniciar sesión para comentar.


Andrei Bobrov
Andrei Bobrov el 14 de Sept. de 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 comentario
Jan
Jan el 14 de Sept. de 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...

Iniciar sesión para comentar.


Jan
Jan el 14 de Sept. de 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 comentarios
Andrei Bobrov
Andrei Bobrov el 14 de Sept. de 2011
Hi Jan!
Small correct:
[value, index] = sort(array(:),'descend');
or
S = value(1);
Sidx = index(1);
L = value(end);
Lidx = index(end);
Jan
Jan el 14 de Sept. de 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.

Iniciar sesión para comentar.


TAB
TAB el 14 de Sept. de 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 comentario
Jan
Jan el 14 de Sept. de 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

Iniciar sesión para comentar.


Jan
Jan el 14 de Sept. de 2011
A fast C-mex for this job: FEX: MinMaxElem

Jan
Jan el 14 de Sept. de 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 comentarios
Sean de Wolski
Sean de Wolski el 14 de Sept. de 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
Jan el 14 de Sept. de 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 .

Iniciar sesión para comentar.


Greg
Greg el 16 de Sept. de 2011
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.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by