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:
Selecting the MAX without accounting for Inf

Subject: Selecting the MAX without accounting for Inf

From: Diego Zegarra

Date: 20 Oct, 2008 20:27:01

Message: 1 of 6

I have a quick question,

Suppose we have a matrix:

A = [67 56 45;
     58 Inf 52;
     49 58 61]

and I want to find the maximum value on the second column, then I use,

max(A(:,2));

Now what if I want it to return the maximum value but not including an Inf number. So in this example the MAX for column 2 of A I want it to return 58.

Thanks in advance and I hope I made myself clear, if not let me know so I can clear any doubts!

Subject: Selecting the MAX without accounting for Inf

From: NZTideMan

Date: 20 Oct, 2008 20:30:46

Message: 2 of 6

On Oct 21, 9:27=A0am, "Diego Zegarra" <diego...@gmail.com> wrote:
> I have a quick question,
>
> Suppose we have a matrix:
>
> A =3D [67 56 45;
> =A0 =A0 =A058 Inf 52;
> =A0 =A0 =A049 58 61]
>
> and I want to find the maximum value on the second column, then I use,
>
> max(A(:,2));
>
> Now what if I want it to return the maximum value but not including an In=
f number. So in this example the MAX for column 2 of A I want it to return =
58.
>
> Thanks in advance and I hope I made myself clear, if not let me know so I=
 can clear any doubts!

max(A(A(:,2)<inf,2))

Subject: Selecting the MAX without accounting for Inf

From: Walter Roberson

Date: 20 Oct, 2008 20:38:51

Message: 3 of 6

Diego Zegarra wrote:

> Suppose we have a matrix:
> and I want to find the maximum value on the second column, then I use,
> max(A(:,2));
> Now what if I want it to return the maximum value but not including an Inf number.

max(A(isfinite(A(:,2)),2);

This code does have a subtle bug, though: if all of the values in the column
are negative infinity or nan, then it will calculate the max as [] instead of as
negative infinity (if they were not all nan) or as nan (if they were all nan)

In general, if your goal is to remove the infinities but to leave the nan in place,
then use ~isinf() instead of isfinite()

Subject: Selecting the MAX without accounting for Inf

From: Diego Zegarra

Date: 20 Oct, 2008 20:39:02

Message: 4 of 6

Thanks NZTideMan! Could you please explain me though what the 2's stand for. I know that the first 2 is to look in the second column, but what about the other one?

> max(A(A(:,2)<inf,2))

Thanks again!

Subject: Selecting the MAX without accounting for Inf

From: Diego Zegarra

Date: 20 Oct, 2008 20:46:02

Message: 5 of 6

Walter thanks for your answer. My matrices will either have positive values or Inf, there are no -Inf or Nan or negative numbers. Could you please explain me though what the second 2 in the code does?
 
> max(A(isfinite(A(:,2)),2);
 
Thanks again!

Subject: Selecting the MAX without accounting for Inf

From: Walter Roberson

Date: 20 Oct, 2008 21:14:09

Message: 6 of 6

Diego Zegarra wrote:
> Could you please explain me though what the second 2 in the code does?
 
>> max(A(isfinite(A(:,2)),2);

Note: I missed a ')' before the ';', it should be

max(A(isfinite(A(:,2)),2))

To understand it, rewrite it as a sequence of steps:

T1 = isfinite(A(:,2));
T2 = A(T1,2);
max(T2)

This makes it clear that both 2's are selecting column 2 of
the matrix A.

The trick involved is that you have to understand "logical indexing",
which you can find more information about in the matlab documentation,
most directly if you search on the phrase
logical types

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