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:
matrix problem with columns

Subject: matrix problem with columns

From: Konst

Date: 13 May, 2012 17:39:08

Message: 1 of 4

I have a matrix(i,2). I want to read the matrix and while the elements of the first column are the same(They are already sorted), compute the mean of the elements in the second row.When the values in column 1 change, do the same but without considering the previous elements of column 2 that have already been used. eg
matrix=1 3
           1 2
           2 1.....
ans=2.5 1.....
Any ideas? Thanks for your time!
           

Subject: matrix problem with columns

From: Konst

Date: 13 May, 2012 17:50:08

Message: 2 of 4

"Konst " <konstance1@hotmail.com> wrote in message <joorjs$70s$1@newscl01ah.mathworks.com>...
> I have a matrix(i,2). I want to read the matrix and while the elements of the first column are the same(They are already sorted), compute the mean of the elements in the second row.When the values in column 1 change, do the same but without considering the previous elements of column 2 that have already been used. eg
> matrix=1 3
> 1 2
> 2 1.....
> ans=2.5 1.....
> Any ideas? Thanks for your time!
>
perhaps if there was a way of putting the elements in the columns of a new matrixes that would be more easy to process...eg here
matrix2=3 2...
matrix3=1.....(and not all of them in one matrix with muptiple columns, in order to avoid the zeros).

Subject: matrix problem with columns

From: Ross W

Date: 13 May, 2012 18:13:08

Message: 3 of 4

"Konst " <konstance1@hotmail.com> wrote in message <joorjs$70s$1@newscl01ah.mathworks.com>...
> I have a matrix(i,2). I want to read the matrix and while the elements of the first column are the same(They are already sorted), compute the mean of the elements in the second row.When the values in column 1 change, do the same but without considering the previous elements of column 2 that have already been used. eg
> matrix=1 3
> 1 2
> 2 1.....
> ans=2.5 1.....
> Any ideas? Thanks for your time!
>

help accumarray
or see http://blogs.mathworks.com/loren/2008/02/20/under-appreciated-accumarray/

Subject: matrix problem with columns

From: Roger Stafford

Date: 13 May, 2012 19:39:10

Message: 4 of 4

"Konst " <konstance1@hotmail.com> wrote in message <joorjs$70s$1@newscl01ah.mathworks.com>...
> I have a matrix(i,2). I want to read the matrix and while the elements of the first column are the same(They are already sorted), compute the mean of the elements in the second row.When the values in column 1 change, do the same but without considering the previous elements of column 2 that have already been used. eg
> matrix=1 3
> 1 2
> 2 1.....
> ans=2.5 1.....
- - - - - - - - - -
  Let M be your matrix:

 f = find([1;diff(M(:,1));1]~=0);
 c = cumsum([0;M(:,2)]);
 A = diff(c(f))./diff(f);

Vector A will be your answer.

  Note: If M has a very large number of rows for which the cumulative round off error in 'cumsum' is excessive, then use 'accumarray' as Ross suggests.

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