Thread Subject: How to average one of column and then duplicate each row to next row?

Subject: How to average one of column and then duplicate each row to next row?

From: Kuo-Hsien

Date: 5 Feb, 2009 15:49:02

Message: 1 of 3

Dear all,

Please advice me some easier method to rearrange this question I'm trying to solve.

Cheers,
Michael

% original data
1 22 333 44 2222
2 33 444 55 4444
3 44 555 66 6666
4 55 666 77 8888
5 66 777 88 2222

% after matlab's arrangement, the new 5th column is the average of original 5th column
1 22 333 44 1111
1 22 333 44 1111
2 33 444 55 2222
2 33 444 55 2222
3 44 555 66 3333
3 44 555 66 3333
4 55 666 77 4444
4 55 666 77 4444
5 66 777 88 1111
5 66 777 88 1111

Subject: How to average one of column and then duplicate each row to next row?

From: Roger Stafford

Date: 5 Feb, 2009 19:07:02

Message: 2 of 3

"Kuo-Hsien" <mchangks@hotmail.com> wrote in message <gmf1pe$jtm$1@fred.mathworks.com>...
> .......
> Please advice me some easier method to rearrange this question I'm trying to solve.
> .......

  On the basis of what I see in your example, it has nothing to do with an "average". You seem to be duplicating each row and dividing the fifth column by two.

  Call x the original data.

 [m,n] = size(x);
 x(:,n) = x(:,n)/2;
 x = reshape(repmat(x(:),1,2).',[],n);

Roger Stafford

Subject: How to average one of column and then duplicate each row to next

From: dpb

Date: 5 Feb, 2009 21:08:15

Message: 3 of 3

Kuo-Hsien wrote:
...
> Please advice me some easier method to rearrange this question I'm trying to solve.
...
Easier than what, pray tell?

> % original data
> 1 22 333 44 2222
> 2 33 444 55 4444
> 3 44 555 66 6666
> 4 55 666 77 8888
> 5 66 777 88 2222
>
> % after matlab's arrangement, the new 5th column is the average of original 5th column
> 1 22 333 44 1111
> 1 22 333 44 1111
> 2 33 444 55 2222
> 2 33 444 55 2222
> 3 44 555 66 3333

The 5th column in the latter certainly isn't an average of the original;
rather it appears to be one-half the original value.

But, if that's what is wanted, something like the following is the
straightforward way...

 >> a = [1 22 333 44 2222;
         2 33 444 55 4444;
         3 44 555 66 6666;
         4 55 666 77 8888;
         5 66 777 88 2222];
 >> b=zeros(2*size(a,2),size(a,1));
 >> nrows=size(a,1);
 >> for i=1:nrows
      i2=2*i-1;
      b(i2,:)=a(i,:);
      b(i2,5)=a(i,5)/2;
      b(i2+1,:)=b(i2,:);
    end
 >> b
b =
            1 22 333 44 1111
            1 22 333 44 1111
            2 33 444 55 2222
            2 33 444 55 2222
            3 44 555 66 3333
            3 44 555 66 3333
            4 55 666 77 4444
            4 55 666 77 4444
            5 66 777 88 1111
            5 66 777 88 1111


--

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com