MATLAB Answers

0

How do I find the two smallest values in varargin?

Asked by Harold Meggs on 24 Apr 2015
Latest activity Answered by David Sanchez
on 24 Apr 2015
For example, the varargin input is 5,3,6
I want to multiply the the two lowest values, 5*3.
I have tried this:
y=sort (varargin,1, 'ascend');
z=y(:, 1:2);
area= z{1}*z{2};
This is the error I receive:
Error using sort
DIM and MODE arguments not supported for cell arrays.

  0 Comments

Sign in to comment.

2 Answers

Answer by David Sanchez
on 24 Apr 2015
 Accepted Answer

function out = two_lowest_values(varargin)
a = sort(cell2mat(varargin));
out = a(1)*a(2);

  0 Comments

Sign in to comment.


Answer by Harold Meggs on 24 Apr 2015

I have also tried this:
Xs = sort(varargin);
min2nd = Xs{1,2};
minVal=min(varargin);
area=min2nd*minVal;
Error message:
Error using sort
Input argument must be a cell array of strings.

  0 Comments

Sign in to comment.