resizing rows into colum

Discussion in 'MATLAB' started by Nickaol, Jul 2, 2009.

  1. Nickaol

    Nickaol Guest

    Hello,

    I have a matrix with 2 rows and 500 columns. I need to reshape it to be 200 rows and 5 columns, with the original matrix chopped off after the 5 column for each row and the remaining moving to the next row.

    for example what I have now is

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
    17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

    i want it to look like
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25
    26 27 28 29 30
    31 32..and so on
     
    Nickaol, Jul 2, 2009
    #1
    1. Advertisements

  2. Nickaol

    Alan B Guest

    X=reshape(X',[5 200])';
     
    Alan B, Jul 2, 2009
    #2
    1. Advertisements

  3. Nickaol

    Nickaol Guest

    Hi Alan,

    thanks for the response..actually I made a mistake in my example...the way it is set up now it is

    1 2 3 4 5 11 12 13 14 15
    6 7 8 9 10 16 17 18 19 20
    21 22 23 24 25 31 32
    26 27 28 29 30

    you see the pattern...?

    So I want it to look like this...

    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25
    26 27 28 29 30
    31 32..and so on

    any ideas?

    Thanks!
     
    Nickaol, Jul 2, 2009
    #3
  4. Nickaol

    Nathan Guest

    Not the most elegant, but...
    A = [1 2 3 4 5 11 12 13 14 15;
    6 7 8 9 10 16 17 18 19 20;
    21 22 23 24 25 31 32 33 34 35;
    26 27 28 29 30 36 37 38 39 40];
    B = [];
    for i =1:numel(A)/length(A)/2
    B = [B;A(i*2-1,1:5);A(i*2,1:5);A(i*2-1,6:10);A(i*2,6:10)];
    end
     
    Nathan, Jul 2, 2009
    #4
  5. Nickaol

    Alan B Guest

    You can do it without for loops:
    A=[1:5 11:15; 6:10 16:20; 21:25 31:35; 26:30 36:40; 41:45 51:55; 46:50 56:60]
    tic
    n=numel(A);
    B=reshape(A,[n/10,5,2]);
    C=permute(B,[2 1 3]);
    D=reshape(C,[10 n/20 2]);
    E=permute(D,[3 2 1]);
    F=reshape(E,[n/10 10])';
    G=reshape(F,[5 n/5])'
    toc

    but the for loop solution is much faster. Maybe its worth it if you can condense some of those reshape and permute calls, otherwise just use the for loop.
     
    Alan B, Jul 2, 2009
    #5
  6. Nickaol

    Jos Guest

    What about:

    A=[1:5 11:15; 6:10 16:20; 21:25 31:35; 26:30 36:40; 41:45 51:55; 46:50 56:60] ;
    B = mat2cell(A,[2 2 2],[5 5]).'
    B = cat(1,B{:})

    hth
    Jos
     
    Jos, Jul 2, 2009
    #6
    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.