Path: news.mathworks.com!newsfeed-00.mathworks.com!kanaga.switch.ch!switch.ch!ecngs!feeder2.ecngs.de!87.106.137.111.MISMATCH!feeder1.news.weretis.net!weretis.net!cs.uu.nl!news.stack.nl!aioe.org!not-for-mail
From: dpb <none@non.net>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to average one of column and then duplicate each row to next
 row?
Date: Thu, 05 Feb 2009 15:08:15 -0600
Organization: Aioe.org NNTP Server
Lines: 54
Message-ID: <gmfkhs$4el$1@aioe.org>
References: <gmf1pe$jtm$1@fred.mathworks.com>
NNTP-Posting-Host: KlTBqVvKbAlgPRl+kXk84Q.user.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Complaints-To: abuse@aioe.org
NNTP-Posting-Date: Thu, 5 Feb 2009 21:09:17 +0000 (UTC)
X-Notice: Filtered by postfilter v. 0.7.7
Cancel-Lock: sha1:7uhv97jvmNDpAmnPkEa4ifyupkA=
User-Agent: Thunderbird 2.0.0.17 (Windows/20080914)
Xref: news.mathworks.com comp.soft-sys.matlab:516460


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


--