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

Discussion in 'MATLAB' started by Kuo-Hsien, Feb 5, 2009.

  1. Kuo-Hsien

    Kuo-Hsien Guest

    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
     
    Kuo-Hsien, Feb 5, 2009
    #1
    1. Advertisements

  2. 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
     
    Roger Stafford, Feb 5, 2009
    #2
    1. Advertisements

  3. Kuo-Hsien

    dpb Guest

    Kuo-Hsien wrote:
    ....
    ....
    Easier than what, pray tell?
    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...
    2 33 444 55 4444;
    3 44 555 66 6666;
    4 55 666 77 8888;
    5 66 777 88 2222];
    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


    --
     
    dpb, Feb 5, 2009
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.