MATLAB Answers


how to find smallest number among 10 numbers in a if loop?

Asked by Sudha
on 22 Apr 2013

Hi all I need find the smallest number among 10 numbers using if loop or for loop.Please need help.

  1 Comment

There is no such thing as an "if loop". "if" executes the body exactly 0 or 1 times, which is not a loop; "for loop" and "while" (usually) execute multiple times.


No products are associated with this question.

1 Answer

Answer by Image Analyst
on 22 Apr 2013
 Accepted answer

Try this:

minValue = inf; % Initialize to infinity
for k = 1 : 10
  if array(k) < minValue;
    % Surely you can fill in the rest!

It's probably too much of a hint but it's hard to do much less since it's so short and trivial.


Jan Simon
on 23 Apr 2013

Calling a variable "linelen5" is a bad idea and a standard mistake of beginners. Use an array instead: "linelen(5)" etc.

linelen = [13, 14, 15, 16, 17, 18, 19, 113, 114, 115, 116]
shortestline = linelen(1)
for k = 2 : length(linelen)


on 26 Apr 2013

Thank u all, i got my result

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!