turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Discussions
- :
- Re: Convert List to Matrix

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Jan 11, 2019 10:30 AM
(241 views)

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

This is pretty fast:

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

9 REPLIES

Highlighted
##
##### Re: Convert List to Matrix

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

Uday

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Jan 11, 2019 1:26 PM
(213 views)
| Posted in reply to message from uday_guntupalli 01/11/2019 04:23 PM

Thanks, I'll give that a try.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Jan 11, 2019 2:02 PM
(205 views)
| Posted in reply to message from uday_guntupalli 01/11/2019 04:23 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

This is pretty fast:

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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!