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
- :
- Re: Convert List to Matrix

- 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
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Jan 11, 2019 10:30 AM
(6798 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
- Mute
- 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 9

Highlighted
##

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

Re: Convert List to Matrix

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
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Convert List to Matrix

Thanks, I'll give that a try.

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

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

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

Re: Convert List to Matrix

- Mark as New
- Bookmark
- Subscribe
- Mute
- 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
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Convert List to Matrix

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

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.

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

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.

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

Re: Convert List to Matrix