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
- :
- Simple Matrix Creation Question

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

Highlighted

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

Jun 19, 2018 6:23 AM
(3552 views)

I have some code where I need to pass a matrix with one value to highlight one row in a table box:

```
nw = new window("test",
tb = table box(
string col box("Col1", {"aa", "bb", "cc"}),
number col box("Col2", [1, 2, 3])
)
);
rm = [2];
tb << set selected rows(rm);
```

The row number to be highlighted is a variable. I would like to use the simplest syntax to create the row number matrix using this variable. These methods don't work:

```
a = 2; // Need a matrix holding this variable value
b = [a]; // Doesn't work
c = [expr(a)]; // No
d = [eval(a)]; // No
e = [eval(expr(a))]; // No
```

These are the methods I came up with that do work:

```
// Works but so complicated...
f = eval(parse(evalinsert("[^a^]"))); // Yes
// Works too
g = [1];
g[1] = a; // Yes
```

Is there a simpler one-liner that I'm missing? I just want to create a matrix like [2] dynamically.

2 ACCEPTED SOLUTIONS

Accepted Solutions

Highlighted

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

I think the Matrix() function will do what you're looking for:

```
//:*/
a=2;
b=matrix(a);
/*:
[2]
```

In your original example:

```
a = 2;
nw = New Window( "test",
tb = Table Box(
String Col Box( "Col1", {"aa", "bb", "cc"} ),
Number Col Box( "Col2", [1, 2, 3] )
)
);
rm = Matrix( a );
tb << set selected rows( rm );
```

-Jeff

Highlighted

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

The original process was to make a matrix from a scalar, so I guess you are asking how to return a scalar from a matrix. Use subscript to access any element in the matrix.

```
// make 1x1 matrix (one element)
a = [2];
// obtain element with a script using row-wise index or [row,col] coordinate
b = a[1];
b = a[1,1];
```

Learn it once, use it forever!

5 REPLIES 5

Highlighted

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

I think the Matrix() function will do what you're looking for:

```
//:*/
a=2;
b=matrix(a);
/*:
[2]
```

In your original example:

```
a = 2;
nw = New Window( "test",
tb = Table Box(
String Col Box( "Col1", {"aa", "bb", "cc"} ),
Number Col Box( "Col2", [1, 2, 3] )
)
);
rm = Matrix( a );
tb << set selected rows( rm );
```

-Jeff

Highlighted
##

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

Re: Simple Matrix Creation Question

Ha - perfect! Thanks Jeff.

Highlighted
##

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

Re: Simple Matrix Creation Question

Hi,

How do I perform this concept in reverse?

a=[2]

b=2

?

Highlighted

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

The original process was to make a matrix from a scalar, so I guess you are asking how to return a scalar from a matrix. Use subscript to access any element in the matrix.

```
// make 1x1 matrix (one element)
a = [2];
// obtain element with a script using row-wise index or [row,col] coordinate
b = a[1];
b = a[1,1];
```

Learn it once, use it forever!

Highlighted
##

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

Re: Simple Matrix Creation Question

Created:
Nov 8, 2019 1:33 PM
| Last Modified: Nov 8, 2019 1:36 PM
(2103 views)
| Posted in reply to message from pmroz 06-19-2018

This works too:

```
a = 2;
// Concat scalar with empty matrix
b = a || [];
// Same, but in place
a ||= [];
Show(a, b);
/*:
a = [2];
b = [2];
```

Article Labels

There are no labels assigned to this post.