Thread Subject: vectorization problem

Subject: vectorization problem

From: Nick Valensi

Date: 20 Nov, 2009 18:21:06

Message: 1 of 3

I've a litte difficulties in tackling a problem in Matlab. I think it's the easiest way to explain it in an example. In the real problem the matrix has like 200000 rows, so it's a bit bigger.

Let's say i've this matrix T :
T=
5 2
5 1.1
5 2.3
6 2
6 3
6 3.1
6 5
7 8
7 8.1
7 17

I want to get the following matrix though. In this matrix a third collumn is added. The value in this column is the minimum of all values in the second row that shear a single value in the first row. I guess this is not really clearly explained, but if you take a look you will get it. The lowest value in the second row after a '5' in the first row is 1.1...etc. So what i want to get is:

5 2 1.1
5 1.1 1.1
5 2.3 1.1
6 2 2
6 3 2
6 3.1 2
6 5 2
7 8 8
7 8.1 8
7 17 8

A very slow option is:
for a=1:size(T,1)
T(a,3)=min(T(find(T(:,1)==T(a,1)),2);
end

I wanted to vectorize this and came up with:
T(:,3)=min(T(find(T(:,1)==T(:,1)),2);
this didn't work though...any ideas are more then welcome!

Subject: vectorization problem

From: Bruno Luong

Date: 20 Nov, 2009 18:36:18

Message: 2 of 3

Try this:

[u I J] = unique(T(:,1));
m = accumarray(J,T(:,2),[],@(x) min(x));
TT = [T m(J)]

% Bruno

Subject: vectorization problem

From: Nick Valensi

Date: 21 Nov, 2009 10:22:01

Message: 3 of 3

Bruno,

I tried your suggestion and guess what, it worked perfect! So thanks a lot for helping me out. -NV-






"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <he6nj2$guo$1@fred.mathworks.com>...
> Try this:
>
> [u I J] = unique(T(:,1));
> m = accumarray(J,T(:,2),[],@(x) min(x));
> TT = [T m(J)]
>
> % Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
vectorize Nick Valensi 20 Nov, 2009 13:24:22
rssFeed for this Thread

Contact us at files@mathworks.com