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:
Finding minimum value associated with particular other values.

Subject: Finding minimum value associated with particular other values.

From: Ulrik Nash

Date: 23 May, 2012 11:16:06

Message: 1 of 4

Hi Everyone,

Suppose I have the following matrix,

A = [1 1 2 3 3 4 4 5 1 2;0.10 0.12 0.34 0.45 0.18 0.34 0.27 0.65 0.48 0.31]

Now I wish to create B, which is a 2 x 10 (in this case) matrix, which holds the minimum values in row 2 of A associated with the value in row 1 of A. So in the example above, the solution is:

B = [1 2 3 4 5;0.10 0.34 0.18 0.27 0.65]

How might this be done?

Subject: Finding minimum value associated with particular other values.

From: Nasser M. Abbasi

Date: 23 May, 2012 11:26:45

Message: 2 of 4

On 5/23/2012 6:16 AM, Ulrik Nash wrote:
> Hi Everyone,
>
> Suppose I have the following matrix,
>
> A = [1 1 2 3 3 4 4 5 1 2;0.10 0.12 0.34 0.45 0.18 0.34 0.27 0.65 0.48 0.31]
>
> Now I wish to create B, which is a 2 x 10 (in this case) matrix, which holds
>the minimum values in row 2 of A associated with the value in row 1 of A.
>So in the example above, the solution is:
>
> B = [1 2 3 4 5;0.10 0.34 0.18 0.27 0.65]
>
> How might this be done?


you mean like this?


---------------------------
I = unique(A(1,:));
B = arrayfun(@(i) min(A(2,A(1,:)==I(i))), 1:length(I));
[I;B]
---------------------------------

1.0000 2.0000 3.0000 4.0000 5.0000
0.1000 0.3100 0.1800 0.2700 0.6500


--Nasser

Subject: Finding minimum value associated with particular other values.

From: Ulrik Nash

Date: 23 May, 2012 11:37:24

Message: 3 of 4

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jpihhk$jet$2@speranza.aioe.org>...
> On 5/23/2012 6:16 AM, Ulrik Nash wrote:
> > Hi Everyone,
> >
> > Suppose I have the following matrix,
> >
> > A = [1 1 2 3 3 4 4 5 1 2;0.10 0.12 0.34 0.45 0.18 0.34 0.27 0.65 0.48 0.31]
> >
> > Now I wish to create B, which is a 2 x 10 (in this case) matrix, which holds
> >the minimum values in row 2 of A associated with the value in row 1 of A.
> >So in the example above, the solution is:
> >
> > B = [1 2 3 4 5;0.10 0.34 0.18 0.27 0.65]
> >
> > How might this be done?
>
>
> you mean like this?
>
>
> ---------------------------
> I = unique(A(1,:));
> B = arrayfun(@(i) min(A(2,A(1,:)==I(i))), 1:length(I));
> [I;B]
> ---------------------------------
>
> 1.0000 2.0000 3.0000 4.0000 5.0000
> 0.1000 0.3100 0.1800 0.2700 0.6500
>
>
> --Nasser

Thank you Nasser.

Subject: Finding minimum value associated with particular other values.

From: Bruno Luong

Date: 23 May, 2012 12:02:06

Message: 4 of 4

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <jpigtm$kmr$1@newscl01ah.mathworks.com>...
> Hi Everyone,
>
> Suppose I have the following matrix,
>
> A = [1 1 2 3 3 4 4 5 1 2;0.10 0.12 0.34 0.45 0.18 0.34 0.27 0.65 0.48 0.31]
>
> Now I wish to create B, which is a 2 x 10 (in this case) matrix, which holds the minimum values in row 2 of A associated with the value in row 1 of A. So in the example above, the solution is:
>
> B = [1 2 3 4 5;0.10 0.34 0.18 0.27 0.65]
>
> How might this be done?

[u, I, J]=unique(A(1,:));
minA = accumarray(J(:),A(2,:)',[],@min);
B = [u; minA']

% Bruno

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