Got Questions? Get Answers.
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

Thread Subject:
min check in array

Subject: min check in array

From: Mike

Date: 28 Oct, 2010 11:45:06

Message: 1 of 15

hi
i have an array a=[1 3 32;5 6 9;12 -4 -2] and i need to find minimum number,but without using min code and desirable using simple loops.

Subject: min check in array

From: Loren Shure

Date: 28 Oct, 2010 11:57:53

Message: 2 of 15


"Mike " <mike10exe@mail.ru> wrote in message
news:iabno2$gii$1@fred.mathworks.com...
> hi
> i have an array a=[1 3 32;5 6 9;12 -4 -2] and i need to find minimum
> number,but without using min code and desirable using simple loops.

Think about using sort.

--
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: min check in array

From: Loren Shure

Date: 28 Oct, 2010 11:58:25

Message: 3 of 15


"Loren Shure" <loren.shure@mathworks.com> wrote in message
news:iabog1$4v1$1@fred.mathworks.com...
>
> "Mike " <mike10exe@mail.ru> wrote in message
> news:iabno2$gii$1@fred.mathworks.com...
>> hi
>> i have an array a=[1 3 32;5 6 9;12 -4 -2] and i need to find minimum
>> number,but without using min code and desirable using simple loops.
>
> Think about using sort.
>
>

of < or > in a loop


--
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: min check in array

From: Enrique Vidal Sanchez

Date: 28 Oct, 2010 12:00:27

Message: 4 of 15

On Oct 28, 1:45 pm, "Mike " <mike10...@mail.ru> wrote:
> hi
> i have an array a=[1 3 32;5 6 9;12 -4 -2] and i need to find minimum number,but without using min code and desirable using simple loops.

You could use sort.

[sorted, trash]=sort(a(:));
minValue = sorted(1);

Regards,
Enrique Vidal

Subject: min check in array

From: Steven_Lord

Date: 28 Oct, 2010 13:20:22

Message: 5 of 15



"Mike " <mike10exe@mail.ru> wrote in message
news:iabno2$gii$1@fred.mathworks.com...
> hi
> i have an array a=[1 3 32;5 6 9;12 -4 -2] and i need to find minimum
> number,but without using min code and desirable using simple loops.

Okay, make it so.

If you need help when writing this function, show the group what you've done
so far and indicate where you're having trouble and what type of trouble
you're having.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: min check in array

From: Matt J

Date: 28 Oct, 2010 13:29:05

Message: 6 of 15

"Mike " <mike10exe@mail.ru> wrote in message <iabno2$gii$1@fred.mathworks.com>...
> hi
> i have an array a=[1 3 32;5 6 9;12 -4 -2] and i need to find minimum number,but without using min code and desirable using simple loops.
=====

minVal=-max(-a);

Subject: min check in array

From: Jan Simon

Date: 28 Oct, 2010 15:05:06

Message: 7 of 15

Dear Matt J,

> minVal = -max(-a);

Funny. I'd accept this as teacher, extra point for efficient problem solving.

What about this:
  AxesH = axes;
  a = [1 3 32;5 6 9;12 -4 -2]
  plot(1:numel(a), transpose(a(:)));
  axis(AxesH, 'tight');
  YL = get(AxesH, 'YLim');
  min = YL(1);

Or:
  a = [1 3 32;5 6 9;12 -4 -2]
  cmp = bsxfun('ge', a(:), transpose(a(:)));
  minValue = v(all(cmp));

Or the old and lean "users are fitter than computers" approach:
  minVal = -4;

Kind regards, Jan

Subject: min check in array

From: Matt J

Date: 28 Oct, 2010 15:24:04

Message: 8 of 15

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <iac3f2$919$1@fred.mathworks.com>...
> Dear Matt J,
>
> > minVal = -max(-a);
>
> Funny. I'd accept this as teacher, extra point for efficient problem solving.
=====

If you didn't explicitly forbid 'max', you'd have to at least accept it wouldn't you?



> What about this:
> AxesH = axes;
> a = [1 3 32;5 6 9;12 -4 -2]
> plot(1:numel(a), transpose(a(:)));
> axis(AxesH, 'tight');
> YL = get(AxesH, 'YLim');
> min = YL(1);
==============

Full marks for originality! (no extra point for efficiency though :))

Subject: min check in array

From: Jan Simon

Date: 28 Oct, 2010 16:56:04

Message: 9 of 15

Dear Matt J,

> If you didn't explicitly forbid 'max', you'd have to at least accept it wouldn't you?

Sorry, I'm German: I cannot follow the stacked negations. I think we agree than the -MAX approach fullfills the question pefectly. ;-)
 

> > YL = get(AxesH, 'YLim');
> Full marks for originality! (no extra point for efficiency though :))

I get the extra-point for (negative) efficiency from my local energy distributor.

If I'm not allowed to use the *function* "min", am I allowed to use the *string* in an EVAL call?
  x = eval('min y')

I think it is time to stop homework quetsions, which contain the term "without using the built-in function xyz". While the rest of the forum tries to find efficient solutions using Matlab's builtin or toolbox functions, these teachers force the beginners not to use Matlab to program in Matlab. Better use PHP or Python, if you want to program in not-Matlab.

Kind regards to the teacher, who has given this home work, Jan

Subject: min check in array

From: Matt J

Date: 29 Oct, 2010 13:58:02

Message: 10 of 15

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <iac9v4$lt9$1@fred.mathworks.com>...
> Dear Matt J,
>
> > If you didn't explicitly forbid 'max', you'd have to at least accept it wouldn't you?
>
> Sorry, I'm German: I cannot follow the stacked negations. I think we agree than the -MAX approach fullfills the question pefectly. ;-)
======

We probably do agree. I just meant that if the teacher did not explicitly say "Don't use MAX" when writing the assignment, then the student is allowed to use MAX. If the teacher later realizes that (s)he should have forbidden MAX, it's the teacher's problem and not the student's. :)

Subject: min check in array

From: Jan Simon

Date: 29 Oct, 2010 17:13:04

Message: 11 of 15

Dear Matt J,
yes, we agree.

As announced, I'm changing my personal CSSM policies now:
I'll do not solve homework problems, *except* if the teacher explicitely forbid to use some specific built-in functions. For the later case I'll try to find every possible work-around and cheat as much as possible.

I'm looking forward to discussions with the teachers. Of course they can teach the pupils how to push a nail in the wall without using a hammer, but e.g. with a screwdriver. Or to draw a picture without a pen, e.g. with some cornflakes.
But I will not support this.

Kind regards, Jan

Subject: min check in array

From: Oleg Komarov

Date: 29 Oct, 2010 18:37:03

Message: 12 of 15

"Jan Simon" wrote in message
> I think it is time to stop homework quetsions, which contain the term "without using the built-in function xyz". While the rest of the forum tries to find efficient solutions using Matlab's builtin or toolbox functions, these teachers force the beginners not to use Matlab to program in Matlab. Better use PHP or Python, if you want to program in not-Matlab.
>
> Kind regards to the teacher, who has given this home work, Jan

I think the assignment is meant to reproduce some kind of "min" algorithm that usually is implemented with low level languages but they use matlab because it easier to use at a beginners level.
Still, this is not the appropriate forum.
1) I would go to a forum where algorithms in general are discussed
2) Then I'll try to implement the function with matlab
3) IFF i have some problems i would ask here

Note that the point in 3) comes last.

Oleg

Subject: min check in array

From: Mitch

Date: 6 Dec, 2010 06:24:04

Message: 13 of 15

"Mike " <mike10exe@mail.ru> wrote in message <iabno2$gii$1@fred.mathworks.com>...
> hi
> i have an array a=[1 3 32;5 6 9;12 -4 -2] and i need to find minimum number,but without using min code and desirable using simple loops.

I actually just did this and stumbled upon your post while trying to figure this out.

While I'm sure your deadline is far past, I'll put this here for other people using it

Finding a minimum in this case is actually pretty simple, first start with a large number (or at least larger than your maximum. b=9999999 is generally a good choice)

Then run a for loop for all the numbers, with a nested if loop comparing each part of the array to that number. If the number in the array is smaller than that number, then have it redefine the variable you set the original large number to this new number. If you do this for all the parts in the array, it will eventually find a minimum.

Finding a maximum would work the same way, just start with a small number (b=-999999) and change the direction of the lessthan sign.



Here's the actual code I used

%a is the row vector you want to find a min for

n = length(a)
b = 99999999

for i = 1:1:n;
     if a(i) < b;
          b = a(i);
     end;
end;

for max

%a is the row vector you want to find a min for

n = length(a)
b = -99999999

for i = 1:1:n;
     if a(i) > b;
          b = a(i);
     end;
end;

You if running them in the same script, you would of course want to have then not both be the variable b

Subject: min check in array

From: Steven_Lord

Date: 6 Dec, 2010 15:04:36

Message: 14 of 15



"Mitch " <blah@blah.com> wrote in message
news:idhvi4$f2t$1@fred.mathworks.com...
> "Mike " <mike10exe@mail.ru> wrote in message
> <iabno2$gii$1@fred.mathworks.com>...
>> hi
>> i have an array a=[1 3 32;5 6 9;12 -4 -2] and i need to find minimum
>> number,but without using min code and desirable using simple loops.
>
> I actually just did this and stumbled upon your post while trying to
> figure this out.
>
> While I'm sure your deadline is far past, I'll put this here for other
> people using it
>
> Finding a minimum in this case is actually pretty simple, first start with
> a large number (or at least larger than your maximum. b=9999999 is
> generally a good choice)

No, it isn't. Your array could contain all values that are greater than b,
in which case your end result would be a minimum that isn't actually present
in the array. A better choice would be b = Inf; there are no double
precision values greater than Inf. [I assume you've already ruled out the
case where a is empty, so that b will be compared with at least one element
from the array.]

> Then run a for loop for all the numbers, with a nested if loop comparing
> each part of the array to that number. If the number in the array is
> smaller than that number, then have it redefine the variable you set the
> original large number to this new number. If you do this for all the parts
> in the array, it will eventually find a minimum.
> Finding a maximum would work the same way, just start with a small number
> (b=-999999) and change the direction of the lessthan sign.

Similarly, use -Inf here.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: min check in array

From: Jan Simon

Date: 6 Dec, 2010 16:20:25

Message: 15 of 15

Dear Mitch,

> Finding a minimum in this case is actually pretty simple, first start with a large number (or at least larger than your maximum. b=9999999 is generally a good choice)

There is the fine number [Inf] in Matlab for such reasons: [Inf] is larger than all numbers.
 
Kind regards, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us