Got Questions? Get Answers.
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 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

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