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:
Problem in finding maximum values in a matrix

Subject: Problem in finding maximum values in a matrix

From: Abdul

Date: 24 Dec, 2008 21:03:02

Message: 1 of 2

I have a matrix z as below:

z=
   [1218 198
    1218 22
    1218 1441
    1218 28
    1219 34
    1219 1789]
     
 elements of matrix z (like 1218 and 1219) in first column are being repeated. I want another matrix which shows these elements only one time (without repetition) with their maximum values (1441 and 1789) in the 2nd column. so the final matrix (suppose p) should be of the form:

p=
  [1218 1441
   1219 1789]
  
I am a novice in MATLAB Programming. Can anybody please help me?
Thanks in advance.

Subject: Problem in finding maximum values in a matrix

From: Donn Shull

Date: 24 Dec, 2008 22:05:04

Message: 2 of 2

"Abdul " <spacian1@hotmail.com> wrote in message <giu826$c1p$1@fred.mathworks.com>...
> I have a matrix z as below:
>
> z=
> [1218 198
> 1218 22
> 1218 1441
> 1218 28
> 1219 34
> 1219 1789]
>
> elements of matrix z (like 1218 and 1219) in first column are being repeated. I want another matrix which shows these elements only one time (without repetition) with their maximum values (1441 and 1789) in the 2nd column. so the final matrix (suppose p) should be of the form:
>
> p=
> [1218 1441
> 1219 1789]
>
> I am a novice in MATLAB Programming. Can anybody please help me?
> Thanks in advance.

Hello Abdul,

There are several matlab functions you should learn about to solve these kind of problems. Try lookinf at help for the following colon, diff, find, and max. Since you want to look at groups based on the first column of z you can start with:

>> col1 = z(:,1)

col1 =

        1218
        1218
        1218
        1218
        1219
        1219

Then you can find where the change happens with diff

>> changeIndex = diff(col1)

changeIndex =

     0
     0
     0
     1
     0

Then find where the change occurs with find

>> index = find(changeIndex)

index =

     4

now make a vector of the values using the colon operator again

>> values = z(:,2)

values =

         198
          22
        1441
          28
          34
        1789

Now use max with the index calculated above

>> firstGroupMax = max(values(1:index))

firstGroupMax =

        1441

>> secondGroupMax = max(values(index+1:end))

secondGroupMax =

        1789

Now use find again to find the index of these values

>> index1 = find(values == firstGroupMax)

index1 =

     3

>> index2 = find(values == secondGroupMax)

index2 =

     6

Now form the new matrix from this information

p = z([index1, index2], 1:2)

p =

        1218 1441
        1219 1789

So that is the information that you need. You should be able to generalize this information and simplify the steps to solve your problem.

Hope this helps,

Donn

Tags for this Thread

No tags are associated with 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