search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview Doc Search Code Search Beta SORRY NOTHING FOUND!
mic
Start speaking... Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# NumPy | asmatrix method

schedule May 21, 2023
Last updated
local_offer
PythonNumPy
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

NumPy's `asmatrix(~)` method constructs a matrix from a sequence of data (e.g. arrays and tuples).

# Parameters

1. `a` | `array-like`

The sequence of data used to construct a matrix.

NOTE

If `a` is a NumPy array or matrix, no copying would be done - modifying the result of `asmatrix(~)` would also automatically modify `a`.

2. `dtype` | `string` or `type` | `optional`

Type of data stored in the NumPy array. By default, the type will be inferred.

A NumPy matrix.

# Examples

## Basic usage

To create a 2 by 2 matrix:

``` np.asmatrix([[4,5],[6,7]]) matrix([[4, 5], [6, 7]]) ```

## Case when array is not copied

Suppose we create a NumPy matrix out of a NumPy array, like so:

``` a = np.array([[4,5],[6,7]])b = np.asmatrix(a) ```

When we modify the contents of `b`:

``` b[0,0] = 9b matrix([[9, 5], [6, 7]]) ```

This would automatically modify `a` as well:

``` a array([[9, 5], [6, 7]]) ```

What's happening here is that the NumPy objects `a` and `b` actually share the same memory space, so modifying one would involve modifying the other. Note that this happens only if we try to call `asmatrix(~)` on a NumPy array or matrix.

thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!