1

How should I accomplish this plotting task task in Matlab?

Thank you.

EDIT: What I am asking is how to plot in Matlab when the data are in one column as described in the link given above.

Regards,

ikel

Community
  • 1
  • 1
ikel
  • 518
  • 2
  • 6
  • 27
  • `M = reshape(X,[],3)` should do the trick to get it into the format you expect based on the linked question. – tmpearce Jan 28 '13 at 03:15
  • @tmpearce I am using MatlabR2012b, I did that initially, but it didn't give me what I wanted. – ikel Jan 28 '13 at 03:22

3 Answers3

4

You could reshape and transpose the matrix and extract the columns:

vec = [1 2 3 4 5 6 7 8 9 10 11 12]';
reshaped_mat = reshape(vec,3,[])';

reshaped_mat will end up looking like this:

 1     2     3
 4     5     6
 7     8     9
10    11    12

And you can extract the columns as follows:

x = reshaped_mat(:,1);
y = reshaped_mat(:,2);
z = reshaped_mat(:,3);
Walfie
  • 3,376
  • 4
  • 32
  • 38
  • Thanks for providing that extra information on extracting the columns individually. – ikel Jan 28 '13 at 08:12
2

You can try something like this >

For example : A=[1 2 3; 4 5 6;7 8 9]

A' would be

     1     4     7
     2     5     8
     3     6     9

First take the transpose,

B = A'

And convert it into a single column,

B(:) would give

ans =

     1
     2
     3
     4
     5
     6
     7
     8
     9

Hope it helps

Kiran
  • 8,034
  • 36
  • 110
  • 176
  • 1
    I think the questioner actually wants to go the other way: from a column to a `Nx3` matrix. – tmpearce Jan 28 '13 at 03:15
  • Oh, I might have got it wrong. Reshape would work right. By the time, I wanted to post it, there is already an answer. – Kiran Jan 28 '13 at 03:31
  • @KiranChandrashekhar upvoted, for giving me the keyword that I needed _"Transpose"_ – ikel Jan 28 '13 at 07:02
2

yet another option for the lazy user: given a vector v

 v = [1 2 3 4 5 6 7 8 9 10 11 12];

since we know the elements go like [x1,y1,z1,x2,y2,z2,...] , plotting x,y,z will probably require plot3, so this is how it can be done directly:

 plot3(v(1:3:end),v(2:3:end),v(3:3:end)) 

where the entries are equivalent to

 x=v(1:3:end);
 y=v(2:3:end);
 z=v(3:3:end);
bla
  • 25,846
  • 10
  • 70
  • 101
  • Thanks for your expertise. Before you called me `lazy` I plotted it by using `plot3()` by digging through examples and manual. – ikel Jan 28 '13 at 09:11
  • Oi! I see my humor did not go well... shame on me. by the way, by lazy i meant my solution (`v(1:3:end)` etc) ... did it at least help somewhat? – bla Jan 28 '13 at 09:23
  • No hard feeling here. I simply didn't get your joke. Thanks for pointing out the `plot3()` and `v`. Your approach was indeed useful and `lazy`. Now that you mentioned it, a lecturer, Rob Miles, in his yellow book speaks of an aspect of programming as __Creativity Laziness__ – ikel Jan 28 '13 at 09:30