Post Reply 
How to convert a single row list to matrix or vector? (Solved)
11-19-2015, 03:18 PM (This post was last modified: 11-19-2015 04:48 PM by cclinus.)
Post: #1
How to convert a single row list to matrix or vector? (Solved)
Hi,
How to convert a single row list to matrix or vector in program?
For example A:={1,2,3,4}

list2mat doesn't work for {1,2,3,4}.
I can use M0:={1,2,3,4} to obtain a vector.
But, I want to use local variable only.
Please help!

Regards,
Find all posts by this user
Quote this message in a reply
11-19-2015, 03:32 PM (This post was last modified: 11-19-2015 03:37 PM by Han.)
Post: #2
RE: How to convert a single row list to matrix or vector?
(11-19-2015 03:18 PM)cclinus Wrote:  Hi,
How to convert a single row list to matrix or vector in program?
For example A:={1,2,3,4}

list2mat doesn't work for {1,2,3,4}.
I can use M0:={1,2,3,4} to obtain a vector.
But, I want to use local variable only.
Please help!

Regards,

list2mat(list, width) - if list has n elements, then this creates a matrix of dimension (n/width) x width

list2mat({1,2,3,4},2) makes a 2x2 matrix using 1,2 for row 1 and 3,4 for row 2.
list2mat({1,2,3,4},4) makes a 1x4 matrix using 1,2,3,4 for the single row. If you want a row vector (i.e. not a 1x4 matrix) you can do something like:

m:=list2mat({1,2,3,4},4);
m(1); // returns just the first row [ 1, 2, 3, 4 ]

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
11-19-2015, 03:34 PM (This post was last modified: 11-19-2015 03:37 PM by Helge Gabert.)
Post: #3
RE: How to convert a single row list to matrix or vector?
TRN(list2mat({1,2,3,4},1)) ? Here you don't need to know n (number of columns). Might be useful in a program.
Find all posts by this user
Quote this message in a reply
11-19-2015, 03:41 PM
Post: #4
RE: How to convert a single row list to matrix or vector?
transpose(list2mat({1,2,3,4},1)) returns [[1 2 3 4 ]]
col(list2mat({1,2,3,4},1),1) returns [1 2 3 4]

   
Find all posts by this user
Quote this message in a reply
11-19-2015, 04:47 PM
Post: #5
RE: How to convert a single row list to matrix or vector?
Thanks everybody!
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)