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:
Quick question

Subject: Quick question

From: Diego Zegarra

Date: 21 Oct, 2008 03:25:03

Message: 1 of 3

Can anyone tell me what is wrong with this logic?

>> M=[6 8 3;9 Inf 7;8 4 5];

>> [A B] = max(diag(M)(isfinite(diag(M,0)),0));

So what I am trying to do is to find the maximum value in the diagonal but not including the Inf's.

So in this case diag(M,0) = [6 Inf 5] so I want MatLab to return me the max finite number, in this case 6.

Thanks in advance for your help!

Subject: Quick question

From: Matt Hughes

Date: 21 Oct, 2008 04:07:56

Message: 2 of 3

I see 3 things wrong:

1) You can't take the index of the return of a function:
diag(M)(isfinite(diag(M,0)) % this doesn't work
What you'll have to do is first put isfinite(diag(M,0) into another variable, then use the indices of that variable.

2) You can't have an index of zero.

3) In this case, max will return only a 1x1 matrix. You don't need two variables to store the result.

The corrected code could look like this:

>> M=[6 8 3;9 Inf 7;8 4 5];
>> c = diag(M);
>> A = max(c(isfinite(diag(M,0)),1))

Subject: Quick question

From: Matt Hughes

Date: 21 Oct, 2008 04:51:49

Message: 3 of 3

My bad, having multiple outputs from max is perfectly valid. Keep your [A, B] = max(...) to get the index in B.

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