BookmarkSubscribe
Choose Language Hide Translation Bar
MarkDayton
Community Trekker

Convert List to Matrix

What is the quickest way to convert a list: {"a", 1, 2, "b"} into a matrix: [. 1 2 .]? 

 

I expect that I can just loop through all of the elements, but I'm hoping there is a quicker way since I will be doing this many thousands of times in my application, with each list containing hundreds of elements. 

1 ACCEPTED SOLUTION

Accepted Solutions
ms
Super User ms
Super User

Re: Convert List to Matrix

This is pretty fast:

MyList = {"a", 1, 2, "b"};
Mat = Number Col Box("", MyList) << get as matrix;
9 REPLIES 9
uday_guntupalli
Community Trekker

Re: Convert List to Matrix

@MarkDayton,

  I don't know if using a data table is ok ? if yes, here you go . Since the data table is private, I would expect it to be more than reasonably fast for hundreds of values

 

MyList = {"a", 1, 2, "b"} ; 

dt = New Table("Temp",
		New Column("C",Numeric,values(MyList)),
		"Private"
              );
Mat = dt << get as matrix; 
Close(dt,"No Save");
Show(Mat);

 

Best
Uday
MarkDayton
Community Trekker

Re: Convert List to Matrix

Thanks, I'll give that a try.

0 Kudos
txnelson
Super User

Re: Convert List to Matrix

Looking at your original question, you wanted a matrix of [1,2].  I have added in a statement to get rid of the missing values

MyList = {"a", 1, 2, "b"} ; 

dt = New Table("Temp",
		New Column("C",Numeric,values(MyList)),
		"Private"
              );
dt << select where( ismissing(:c)) << delete rows;
Mat = dt << get as matrix; 
Close(dt,"No Save");
Show(Mat);
Jim
0 Kudos
MarkDayton
Community Trekker

Re: Convert List to Matrix

Actually I need the missing values. Sometimes a missing value isn't a problem, for example a test is added later on creating additional columns in the dataset. Earlier data isn't out of spec for tests that weren't performed. Unfortunately, sometimes missing data is a problem, test aborts but prior results are in spec. I haven't figured out how to deal with that yet, but I am making progress. 

0 Kudos
ms
Super User ms
Super User

Re: Convert List to Matrix

This is pretty fast:

MyList = {"a", 1, 2, "b"};
Mat = Number Col Box("", MyList) << get as matrix;
MarkDayton
Community Trekker

Re: Convert List to Matrix

Practically perfect! Question, what happens to the Number Col Box? Does it stay in memory, or is it only created long enough to send the message to?


0 Kudos
ms
Super User ms
Super User

Re: Convert List to Matrix

Not sure about memory handling here. But since the NumberColBox isn't drawn or stored in a variable I don't think it uses or locks up significant amounts of memory.

 

It also seems to scale linearly: If looped 10000x , 100000x and 1000000x it takes 0.27, 2.7 and 26.7 seconds, respectively. No difference in memory usage.

 

Applied to this small list, the number col box is about 30 times faster than the data table approach posted above. For very long lists – there may be an upper limit for how many items a NumberColBox can hold – the data table method may be more efficient. 

MarkDayton
Community Trekker

Re: Convert List to Matrix

To be perfect, I did this:

 

data = shape(Number Col Box("", dt[1,0]) << get as matrix, 1, N Cols(dt));

Now, given similarly constructed matrices for the LSL/USL specs, I can do:

If( max(data < LSL) > 0 | max(data > USL) > 0, "FAIL", "PASS")

to check a row of data against the limits specified in the column properties, without having to loop through the columns for every row.

MarkDayton
Community Trekker

Re: Convert List to Matrix

BOOYAH!, Declaring victory and going home! On my data table with 30,000 rows and 150 columns, my routine went from a runtime of 8.5 minutes looping through every column on every row and checking against the limits for that column, to a runtime of 4.5 SECONDS checking a whole row in one shot! As I said, BOOYAH!