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:
How to find maximum values in a matrix

Subject: How to find maximum values in a matrix

From: Abdul

Date: 24 Dec, 2008 20:32:01

Message: 1 of 8

I have a matrix z as below:

       z =

        1218 198
        1218 22
        1218 1441
        1218 28
        1219 34
        1219 1789

some elements of matrix z (like 1218 and 1219) in first column are being repeated. I want another matrix which shows these elements (1218 and 1219) only one time 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: How to find maximum values in a matrix

From: Nasser Abbasi

Date: 24 Dec, 2008 21:01:48

Message: 2 of 8


"Abdul " <spacian1@hotmail.com> wrote in message
news:giu681$21u$1@fred.mathworks.com...
>I have a matrix z as below:
>
> z =
>
> 1218 198
> 1218 22
> 1218 1441
> 1218 28
> 1219 34
> 1219 1789
>
> some elements of matrix z (like 1218 and 1219) in first column are being
> repeated. I want another matrix which shows these elements (1218 and 1219)
> only one time 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.

one way among many I am sure:

clear all;
A= [1218 198
      1218 22
      1218 1441
      1218 28
      1219 34
      1219 1789];

B=sortrows(A,-2);
[v,I]=unique(B(:,1),'first');
B(I,:)

ans =
        1218 1441
        1219 1789

--Nasser

Subject: How to find maximum values in a matrix

From: Abdul

Date: 24 Dec, 2008 21:48:01

Message: 3 of 8

Dear Naseer,

Thanks for the reply,
but the programme u have told is not working, i don't know why.
can u please give some more help to solve it?
regards


"Nasser Abbasi" <nma@12000.org> wrote in message <9rx4l.15441$ZP4.9441@nlpi067.nbdc.sbc.com>...
>
> "Abdul " <spacian1@hotmail.com> wrote in message
> news:giu681$21u$1@fred.mathworks.com...
> >I have a matrix z as below:
> >
> > z =
> >
> > 1218 198
> > 1218 22
> > 1218 1441
> > 1218 28
> > 1219 34
> > 1219 1789
> >
> > some elements of matrix z (like 1218 and 1219) in first column are being
> > repeated. I want another matrix which shows these elements (1218 and 1219)
> > only one time 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.
>
> one way among many I am sure:
>
> clear all;
> A= [1218 198
> 1218 22
> 1218 1441
> 1218 28
> 1219 34
> 1219 1789];
>
> B=sortrows(A,-2);
> [v,I]=unique(B(:,1),'first');
> B(I,:)
>
> ans =
> 1218 1441
> 1219 1789
>
> --Nasser
>

Subject: How to find maximum values in a matrix

From: Nasser Abbasi

Date: 24 Dec, 2008 21:51:20

Message: 4 of 8


"Abdul " <spacian1@hotmail.com> wrote in message
news:giuamh$q9o$1@fred.mathworks.com...
> Dear Naseer,
>
> Thanks for the reply,
> but the programme u have told is not working, i don't know why.
> can u please give some more help to solve it?
> regards
>

You must have typed something wrong in copying it? It works just fine on my
Matlab 2008b and here it is again below.

What exactly is the error you got? Post everything you typed and the error
message

>>
>> clear all;
>> A= [1218 198
>> 1218 22
>> 1218 1441
>> 1218 28
>> 1219 34
>> 1219 1789];
>>
>> B=sortrows(A,-2);
>> [v,I]=unique(B(:,1),'first');
>> B(I,:)
>>
>> ans =
>> 1218 1441
>> 1219 1789
>>


--Nasser

Subject: How to find maximum values in a matrix

From: Abdul

Date: 24 Dec, 2008 22:53:01

Message: 5 of 8

"Nasser Abbasi" <nma@12000.org> wrote in message <B9y4l.15443$ZP4.6360@nlpi067.nbdc.sbc.com>...
>
> "Abdul " <spacian1@hotmail.com> wrote in message
> news:giuamh$q9o$1@fred.mathworks.com...
> > Dear Naseer,
> >
> > Thanks for the reply,
> > but the programme u have told is not working, i don't know why.
> > can u please give some more help to solve it?
> > regards
> >
>
> You must have typed something wrong in copying it? It works just fine on my
> Matlab 2008b and here it is again below.
>
> What exactly is the error you got? Post everything you typed and the error
> message
>
> >>
--> >> clear all;
> >> A= [1218 198
> >> 1218 22
> >> 1218 1441
> >> 1218 28
> >> 1219 34
> >> 1219 1789];
> >>
> >> B=sortrows(A,-2);
> >> [v,I]=unique(B(:,1),'first');
> >> B(I,:)
> >>
> >> ans =
> >> 1218 1441
> >> 1219 1789
> >>
>
>
> --Nasser
>
Hi Naseer,

After running the program through M-file (trial.m), it gives all below with error message:

echo on
A=[1218,198;1218,22;1218,1441;1218,28;1219,34;1219,1789]

A =

        1218 198
        1218 22
        1218 1441
        1218 28
        1219 34
        1219 1789

B=sortrows(A,-2)

B =

        1219 1789
        1218 1441
        1218 198
        1219 34
        1218 28
        1218 22

[v,I]=unique(B(:,1),'first')
??? Error using ==> unique
Unknown flag.

Error in ==> trial at 6
[v,I]=unique(B(:,1),'first')

Regards and thanks a lot,

Abdul

Subject: How to find maximum values in a matrix

From: Nasser Abbasi

Date: 24 Dec, 2008 22:57:14

Message: 6 of 8


"Abdul " <spacian1@hotmail.com> wrote in message
news:giuegd$noo$1@fred.mathworks.com...


> Hi Naseer,
>
> After running the program through M-file (trial.m), it gives all below
> with error message:
>
> echo on
> A=[1218,198;1218,22;1218,1441;1218,28;1219,34;1219,1789]
>
> A =
>
> 1218 198
> 1218 22
> 1218 1441
> 1218 28
> 1219 34
> 1219 1789
>
> B=sortrows(A,-2)
>
> B =
>
> 1219 1789
> 1218 1441
> 1218 198
> 1219 34
> 1218 28
> 1218 22
>
> [v,I]=unique(B(:,1),'first')
> ??? Error using ==> unique
> Unknown flag.
>
> Error in ==> trial at 6
> [v,I]=unique(B(:,1),'first')
>
> Regards and thanks a lot,
>
> Abdul

Strange. I do not get this error on Matlab 2008b

Can you type the command

EDU>> which unique
C:\Program Files\MATLAB\R2008b Student\toolbox\matlab\ops\unique.m
EDU>>

And see what you get?
Which version of Matlab and which Platform you have?

May be this flag to unique was added in later version than what you have.

--Nasser

Subject: How to find maximum values in a matrix

From: Abdul

Date: 24 Dec, 2008 23:13:02

Message: 7 of 8

"Nasser Abbasi" <nma@12000.org> wrote in message <m7z4l.15448$ZP4.7004@nlpi067.nbdc.sbc.com>...
>
> "Abdul " <spacian1@hotmail.com> wrote in message
> news:giuegd$noo$1@fred.mathworks.com...
>
>
> > Hi Naseer,
> >
> > After running the program through M-file (trial.m), it gives all below
> > with error message:
> >
> > echo on
> > A=[1218,198;1218,22;1218,1441;1218,28;1219,34;1219,1789]
> >
> > A =
> >
> > 1218 198
> > 1218 22
> > 1218 1441
> > 1218 28
> > 1219 34
> > 1219 1789
> >
> > B=sortrows(A,-2)
> >
> > B =
> >
> > 1219 1789
> > 1218 1441
> > 1218 198
> > 1219 34
> > 1218 28
> > 1218 22
> >
> > [v,I]=unique(B(:,1),'first')
> > ??? Error using ==> unique
> > Unknown flag.
> >
> > Error in ==> trial at 6
> > [v,I]=unique(B(:,1),'first')
> >
> > Regards and thanks a lot,
> >
> > Abdul
>
> Strange. I do not get this error on Matlab 2008b
>
> Can you type the command
>
> EDU>> which unique
> C:\Program Files\MATLAB\R2008b Student\toolbox\matlab\ops\unique.m
> EDU>>
>
> And see what you get?
> Which version of Matlab and which Platform you have?
>
> May be this flag to unique was added in later version than what you have.
>
> --Nasser
>
Hi Naseer,


> EDU>> which unique
I get the following with above command,

C:\MATLAB7\toolbox\matlab\ops\unique.m


>> ver
and with the above command ver i get


-------------------------------------------------------------------------------------
MATLAB Version 7.0.0.19920 (R14)
MATLAB License Number: 0
Operating System: Microsoft Windows XP Version 5.1 (Build 2600: Service Pack 3)
Java VM Version: Java 1.4.2 with Sun Microsystems Inc. Java HotSpot(TM) Client VM


regards,
Abdul

Subject: How to find maximum values in a matrix

From: Roger Stafford

Date: 24 Dec, 2008 23:18:01

Message: 8 of 8

"Abdul " <spacian1@hotmail.com> wrote in message <giu681$21u$1@fred.mathworks.com>...
> I have a matrix z as below:
> ........

Try this:

 p = sortrows(z);
 p = p([diff(p(:,1)~=0;true],:);

Roger Stafford

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