1
votes

I have large array. Now I need a matrix with 8 elements in every row. My array looks like this:

    A= Columns 1 through 18

0    0    0    0    0    0    0    0    0    0    1    1    0    1    1    1    0    0

Columns 19 through 36

0    0    0    0    0    0    0    0    0    1    0    0    0    0    1    1    0    0

and so on. How I can get [nx8] matrix? For example:

    B=[0    0    0    0    0    0    0    0
       0    0    1    1    0    1    1    1
       0    0    0    0    0    0    0    0
       0    0    0    1    0    0    0    0]

I've tried reshape, but it didn't work correctly. I get one 1 where shouldn't be.

    B=reshape(A,[],8)
2
8*4 = 32, not 36. The sizes aren't compatible. You can do reshape(A,[4 9]) thoughPatrick
@Patrick I'm assuming that "and so on" means it end on a multiple of 8.chappjc

2 Answers

6
votes

You almost have it. The problem is that Matlab fills the matrix column-wise, whereas you seem to want it filled row-rise. So create an 8-row matrix and then transpose:

reshape(A,8,[]).'
4
votes

What about vec2mat vec2mat

 vec2mat(A,8)