1

In one file 'file.mat', I have a matrix which its size is (1,100), it is written vertically like this:

  M1 =

     Columns 1 through 26:

        6   13    3   15   13   12    8    5    5    1   11    8    5    9    1    7   15    9    2    5    7    7    3    9    0   13

     Columns 27 through 52:

        4    5    7    2    6    6    2    7   12    5    5   12    0    6   11   15    1    2   12    9   13    9    7   13    2    2

     Columns 53 through 78:

        7   15    4   15    5   12    5   12   14    3   10   15   12    5    5   15    3    3    9    3    6    0   13   13    8    5

     Columns 79 through 100:

        2   10    0    8    5    5    9    8   13   14   15   14   10    6    7    8    9   10   14    5    2    5

How to change it in an horizontal Matrix?

twknab
  • 1,741
  • 1
  • 21
  • 35

2 Answers2

3

You can use M1.' or permute(M1,[2 1]). If you want all numbers to be in one horizontal line (i.e. to be an vector) you can use reshape(M1, [1,100])

StefanM
  • 797
  • 1
  • 10
  • 17
  • 2
    Tips: `reshape(M1, 1, [])` will do what you want. If you use `[]` MATLAB will select the dimensions so that it matches. That way you don't risk making dimension mismatch errors. By the way: `M.'` is the correct transpose operator, not `'`. – Stewie Griffin Sep 08 '16 at 13:39
  • The input matrix is purely real. As no complex number is contained M1' and M1.' will give the same result. – StefanM Sep 08 '16 at 13:45
  • 3
    True, but it's a common pitfall for new users. And, users posting questions here often use simplified data, when their real data is more ... _Complex_. – Stewie Griffin Sep 08 '16 at 13:46
  • @StewieGriffin I don't want to get into a flame war on the two transposes, so I'll say my bit once here and move on: Users should take transpose for if they are doing it for mathematical reasons not input shaping reasons. In that case, it's usually mathematically correct to take the conjugate transpose not the straight transpose. If it's just reshaping data, then they should use `reshape` or just insist that the inputs be formed properly in the first place. – Brick Sep 08 '16 at 13:50
  • 2
    @Brick I'm not up for a flame war either, although I'm quite sure I would win! I have constructed advanced fighter-jets, mind control devices, a weather control device, a teleportation device, robots, clones, a working Transporter device from Star Trek, time machines, a Multiverse Transporter, a shrinking pod, as well as an assortment of guns including lasers, rocket launchers, and crossbows. You wouldn't stand a chance... :) Of course, when it comes to the question about `'` vs `.'` we wouldn't advance much :P – Stewie Griffin Sep 08 '16 at 13:52
3

What you have is a horizontal vector, but MATLAB displays it like that so that you can easily see where each element belongs. I guess what you want is to display the vector as a horizontal vector, so that you can copy-paste it. If so:

You can use sprintf if you want to display this as a long vector.

sprintf('%i ', M)
ans =   
35 3 31 8 30 4 1 32 9 28 5 36 6 7 2 33 34 29 26 21 22 17 12 13 19 23 27 10 14 18 24 25 20 15 16 11 

Or if you need the brackets:

['[', sprintf('%i ', M), ']']    
ans =    
[35 3 31 8 30 4 1 32 9 28 5 36 6 7 2 33 34 29 26 21 22 17 12 13 19 23 27 10 14 18 24 25 20 15 16 11 ]

You can also have it tab-separated: sprintf('%i\t', M), or with commas: sprintf('%i,', M).

If you want to reshape your horizontal vector to a vertical, you can do:

M = M.';

Note that ' is NOT the transpose operator, .' is. If you have a vector, but don't know whether it's horizontal of vertical, use the following notation: M = M(:).', or reshape(M, 1, []).

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • This changes the way that it is printed. It doesn't change the matrix structure. OP was maybe a bit ambiguous but this doesn't seem to be what he wants. – Brick Sep 08 '16 at 13:26
  • I can't know for sure, but I believe OP's problem is that he/she wants to see the vector as a line, not on separate rows. – Stewie Griffin Sep 08 '16 at 13:44
  • If he just wants to print, then your answer is good. Of course if he wants to change the matrix, then he likely wants a conjugate transpose - which goes against your answer all the way through, including the point about `.'` vs. `'`. Maybe he'll clarify. There are some cases where you want a regular transpose, but more usually the conjugate transpose is the mathematically correct choice when you have complex numbers. – Brick Sep 08 '16 at 13:47