1

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]) though Commented Jan 2, 2014 at 22:36
  • @Patrick I'm assuming that "and so on" means it end on a multiple of 8. Commented Jan 3, 2014 at 1:16

2 Answers 2

6

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,[]).'
Sign up to request clarification or add additional context in comments.

Comments

4

What about vec2mat vec2mat

 vec2mat(A,8)

4 Comments

Nice and simple! Why the f___ didn't I know about this function?
@chappjc: well, it's part of a not so common toolbox. So actually not a very general solution.
@thewaywewalk That's why. I've never used or seen the communications system toolbox.
@chappjc That toolbox also has the very interesting (and quite new to me also) de2bi and bi2de, which are like dec2bin and bin2dec but using numeric vectors instead of strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.