numpy reshape question (matlab comparison)Obtaining an invertible square matrix from a non-square matrix of full rank in numpy or matlabDump a NumPy array into a csv fileCreate numpy array from matrix declared inside .m matlab fileWriting functions that accept both 1-D and 2-D numpy arrays?Why is numpy much slower than matlab on a digitize example?ndim in numpy array loaded with scipy.io.loadmat?MATLAB: how to stack up arrays “shape-agnostically”?How to assign a value to specific locations of a matrix in MATLAB?What does -1 mean in numpy reshape?MATLAB: Matrix to Vector row-wise
If I arrive in the UK, and then head to mainland Europe, does my Schengen visa 90 day limit start when I arrived in the UK, or mainland Europe?
The Most Powerful Number
On San Andreas Speedruns, why do players blow up the Picador in the mission Ryder?
What is the recommended procedure to land a taildragger in a crosswind?
Can a UK national work as a paid shop assistant in the USA?
How would a developer who mostly fixed bugs for years at a company call out their contributions in their CV?
Co-author wants to put their current funding source in the acknowledgements section because they edited the paper
What are these clip-like things?
Security vulnerabilities of POST over SSL
New input cell style with a custom evaluator
How does the Earth's center produce heat?
Interpreation ROC AUC score
Why is unzipped directory exactly 4.0k (much smaller than zipped file)?
Rear caliper seized after brake pads replaced. Is this linked?
Why was this character made Grand Maester?
Testing using real data of the customer
Why is the Eisenstein ideal paper so great?
Dad jokes are fun
Count all vowels in string
How can I prevent holy aura from a human body from radiating too strongly?
Why does FOO=bar; export the variable into my environment
Is there a simple example that empirical evidence is misleading?
Shorten or merge multiple lines of `&> /dev/null &`
How to melt snow without fire or using body heat?
numpy reshape question (matlab comparison)
Obtaining an invertible square matrix from a non-square matrix of full rank in numpy or matlabDump a NumPy array into a csv fileCreate numpy array from matrix declared inside .m matlab fileWriting functions that accept both 1-D and 2-D numpy arrays?Why is numpy much slower than matlab on a digitize example?ndim in numpy array loaded with scipy.io.loadmat?MATLAB: how to stack up arrays “shape-agnostically”?How to assign a value to specific locations of a matrix in MATLAB?What does -1 mean in numpy reshape?MATLAB: Matrix to Vector row-wise
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Say I have a 1-D vector named s composed of 0,3,6,9.
In MATLAB the shape is denoted (1,4). i.e. a 1x4 row vector.
But in numpy the shape is given as (4,). Why? Shouldn't this notation denote a 4x1 vector, since python also uses the row x col convention?
Now if I want to reshape the row vector, in MATLAB I would type reshape(s,[4,1]) to get a column vector.
I would assume the standard notation for an equivalent operation is s.reshape(4,1). But in the documentation I see s.reshape(-1,1). Why? Is one syntax better than the other? What does -1 mean in this context?
matlab numpy reshape
add a comment |
Say I have a 1-D vector named s composed of 0,3,6,9.
In MATLAB the shape is denoted (1,4). i.e. a 1x4 row vector.
But in numpy the shape is given as (4,). Why? Shouldn't this notation denote a 4x1 vector, since python also uses the row x col convention?
Now if I want to reshape the row vector, in MATLAB I would type reshape(s,[4,1]) to get a column vector.
I would assume the standard notation for an equivalent operation is s.reshape(4,1). But in the documentation I see s.reshape(-1,1). Why? Is one syntax better than the other? What does -1 mean in this context?
matlab numpy reshape
1
numpy has real 1d arrays,
– hpaulj
Mar 23 at 23:21
1
rows and columns are convenient descriptors for 2d arrays. They are not formally defined or used. numpy always talks about axes.
– hpaulj
Mar 24 at 0:19
That-1in reshape is 'what ever works'. MATLAB uses [] for that.
– hpaulj
Mar 24 at 1:15
add a comment |
Say I have a 1-D vector named s composed of 0,3,6,9.
In MATLAB the shape is denoted (1,4). i.e. a 1x4 row vector.
But in numpy the shape is given as (4,). Why? Shouldn't this notation denote a 4x1 vector, since python also uses the row x col convention?
Now if I want to reshape the row vector, in MATLAB I would type reshape(s,[4,1]) to get a column vector.
I would assume the standard notation for an equivalent operation is s.reshape(4,1). But in the documentation I see s.reshape(-1,1). Why? Is one syntax better than the other? What does -1 mean in this context?
matlab numpy reshape
Say I have a 1-D vector named s composed of 0,3,6,9.
In MATLAB the shape is denoted (1,4). i.e. a 1x4 row vector.
But in numpy the shape is given as (4,). Why? Shouldn't this notation denote a 4x1 vector, since python also uses the row x col convention?
Now if I want to reshape the row vector, in MATLAB I would type reshape(s,[4,1]) to get a column vector.
I would assume the standard notation for an equivalent operation is s.reshape(4,1). But in the documentation I see s.reshape(-1,1). Why? Is one syntax better than the other? What does -1 mean in this context?
matlab numpy reshape
matlab numpy reshape
asked Mar 23 at 23:10
kitskits
357316
357316
1
numpy has real 1d arrays,
– hpaulj
Mar 23 at 23:21
1
rows and columns are convenient descriptors for 2d arrays. They are not formally defined or used. numpy always talks about axes.
– hpaulj
Mar 24 at 0:19
That-1in reshape is 'what ever works'. MATLAB uses [] for that.
– hpaulj
Mar 24 at 1:15
add a comment |
1
numpy has real 1d arrays,
– hpaulj
Mar 23 at 23:21
1
rows and columns are convenient descriptors for 2d arrays. They are not formally defined or used. numpy always talks about axes.
– hpaulj
Mar 24 at 0:19
That-1in reshape is 'what ever works'. MATLAB uses [] for that.
– hpaulj
Mar 24 at 1:15
1
1
numpy has real 1d arrays,
– hpaulj
Mar 23 at 23:21
numpy has real 1d arrays,
– hpaulj
Mar 23 at 23:21
1
1
rows and columns are convenient descriptors for 2d arrays. They are not formally defined or used. numpy always talks about axes.
– hpaulj
Mar 24 at 0:19
rows and columns are convenient descriptors for 2d arrays. They are not formally defined or used. numpy always talks about axes.
– hpaulj
Mar 24 at 0:19
That
-1 in reshape is 'what ever works'. MATLAB uses [] for that.– hpaulj
Mar 24 at 1:15
That
-1 in reshape is 'what ever works'. MATLAB uses [] for that.– hpaulj
Mar 24 at 1:15
add a comment |
1 Answer
1
active
oldest
votes
Step back from numpy a moment and look at Python lists:
In [165]: alist = [0,3,6,9]
In [166]: alist
Out[166]: [0, 3, 6, 9]
In [167]: alist[1]
Out[167]: 3
This 3 is a scalar; I'd get an error if I tried index it, alist[1][0].
Now make a list of lists:
In [168]: alist = [[0],[3],[6],[9]]
In [169]: alist
Out[169]: [[0], [3], [6], [9]]
In [170]: alist[1]
Out[170]: [3]
In [171]: alist[1][0]
Out[171]: 3
I can index it twice.
In Octave, the poor man's MATLAB
>> x = [0,3,6,9];
>> x(2)
ans = 3
>> size(x)
ans =
1 4
>> size(x(2))
ans =
1 1
x(2) is still a 2d matrix; I could index it indefinitely, x(2)(1)(1)(1). Size itself is a 2d matrix; everything in MATLAB is 2d (or higher).
>> size(size(x))
ans =
1 2
Back in Python/numpy:
In [172]: arr = np.array([0,3,6,9])
In [173]: arr.shape
Out[173]: (4,) # a 1 element tuple
In [175]: arr[1]
Out[175]: 3
In [176]: type(Out[175])
Out[176]: numpy.int64
In [177]: Out[175].shape
Out[177]: ()
The result of indexing an element of that 1d array is a numpy scalar object, with a 0d shape. https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
Judging from many questions, it seems that MATLAB users have trouble conceiving of an array with 1 or even 0 dimensions. That lower 2d bound becomes thoroughly ingrained in their thinking. It also seems to be foundational to some (if not all) versions of linear algebra. There are matrices, and row vectors and column vectors, but not 'plain' vectors.
But numpy runs in Python, and the behavior of its arrays is consistent with Python lists. And logically consistent with itself.
Here's what a 'column' vector and 'row' vector look like. Note the shapes - both 2 element tuples. And nesting of brackets (2 levels). The similarity to nested list is intentional.
In [178]: arr = np.array([[0],[3],[6],[9]])
In [179]: arr.shape
Out[179]: (4, 1)
In [180]: arr
Out[180]:
array([[0],
[3],
[6],
[9]])
In [181]: arr = np.array([[0,3,6,9]])
In [182]: arr.shape
Out[182]: (1, 4)
In [183]: arr
Out[183]: array([[0, 3, 6, 9]])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55319224%2fnumpy-reshape-question-matlab-comparison%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Step back from numpy a moment and look at Python lists:
In [165]: alist = [0,3,6,9]
In [166]: alist
Out[166]: [0, 3, 6, 9]
In [167]: alist[1]
Out[167]: 3
This 3 is a scalar; I'd get an error if I tried index it, alist[1][0].
Now make a list of lists:
In [168]: alist = [[0],[3],[6],[9]]
In [169]: alist
Out[169]: [[0], [3], [6], [9]]
In [170]: alist[1]
Out[170]: [3]
In [171]: alist[1][0]
Out[171]: 3
I can index it twice.
In Octave, the poor man's MATLAB
>> x = [0,3,6,9];
>> x(2)
ans = 3
>> size(x)
ans =
1 4
>> size(x(2))
ans =
1 1
x(2) is still a 2d matrix; I could index it indefinitely, x(2)(1)(1)(1). Size itself is a 2d matrix; everything in MATLAB is 2d (or higher).
>> size(size(x))
ans =
1 2
Back in Python/numpy:
In [172]: arr = np.array([0,3,6,9])
In [173]: arr.shape
Out[173]: (4,) # a 1 element tuple
In [175]: arr[1]
Out[175]: 3
In [176]: type(Out[175])
Out[176]: numpy.int64
In [177]: Out[175].shape
Out[177]: ()
The result of indexing an element of that 1d array is a numpy scalar object, with a 0d shape. https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
Judging from many questions, it seems that MATLAB users have trouble conceiving of an array with 1 or even 0 dimensions. That lower 2d bound becomes thoroughly ingrained in their thinking. It also seems to be foundational to some (if not all) versions of linear algebra. There are matrices, and row vectors and column vectors, but not 'plain' vectors.
But numpy runs in Python, and the behavior of its arrays is consistent with Python lists. And logically consistent with itself.
Here's what a 'column' vector and 'row' vector look like. Note the shapes - both 2 element tuples. And nesting of brackets (2 levels). The similarity to nested list is intentional.
In [178]: arr = np.array([[0],[3],[6],[9]])
In [179]: arr.shape
Out[179]: (4, 1)
In [180]: arr
Out[180]:
array([[0],
[3],
[6],
[9]])
In [181]: arr = np.array([[0,3,6,9]])
In [182]: arr.shape
Out[182]: (1, 4)
In [183]: arr
Out[183]: array([[0, 3, 6, 9]])
add a comment |
Step back from numpy a moment and look at Python lists:
In [165]: alist = [0,3,6,9]
In [166]: alist
Out[166]: [0, 3, 6, 9]
In [167]: alist[1]
Out[167]: 3
This 3 is a scalar; I'd get an error if I tried index it, alist[1][0].
Now make a list of lists:
In [168]: alist = [[0],[3],[6],[9]]
In [169]: alist
Out[169]: [[0], [3], [6], [9]]
In [170]: alist[1]
Out[170]: [3]
In [171]: alist[1][0]
Out[171]: 3
I can index it twice.
In Octave, the poor man's MATLAB
>> x = [0,3,6,9];
>> x(2)
ans = 3
>> size(x)
ans =
1 4
>> size(x(2))
ans =
1 1
x(2) is still a 2d matrix; I could index it indefinitely, x(2)(1)(1)(1). Size itself is a 2d matrix; everything in MATLAB is 2d (or higher).
>> size(size(x))
ans =
1 2
Back in Python/numpy:
In [172]: arr = np.array([0,3,6,9])
In [173]: arr.shape
Out[173]: (4,) # a 1 element tuple
In [175]: arr[1]
Out[175]: 3
In [176]: type(Out[175])
Out[176]: numpy.int64
In [177]: Out[175].shape
Out[177]: ()
The result of indexing an element of that 1d array is a numpy scalar object, with a 0d shape. https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
Judging from many questions, it seems that MATLAB users have trouble conceiving of an array with 1 or even 0 dimensions. That lower 2d bound becomes thoroughly ingrained in their thinking. It also seems to be foundational to some (if not all) versions of linear algebra. There are matrices, and row vectors and column vectors, but not 'plain' vectors.
But numpy runs in Python, and the behavior of its arrays is consistent with Python lists. And logically consistent with itself.
Here's what a 'column' vector and 'row' vector look like. Note the shapes - both 2 element tuples. And nesting of brackets (2 levels). The similarity to nested list is intentional.
In [178]: arr = np.array([[0],[3],[6],[9]])
In [179]: arr.shape
Out[179]: (4, 1)
In [180]: arr
Out[180]:
array([[0],
[3],
[6],
[9]])
In [181]: arr = np.array([[0,3,6,9]])
In [182]: arr.shape
Out[182]: (1, 4)
In [183]: arr
Out[183]: array([[0, 3, 6, 9]])
add a comment |
Step back from numpy a moment and look at Python lists:
In [165]: alist = [0,3,6,9]
In [166]: alist
Out[166]: [0, 3, 6, 9]
In [167]: alist[1]
Out[167]: 3
This 3 is a scalar; I'd get an error if I tried index it, alist[1][0].
Now make a list of lists:
In [168]: alist = [[0],[3],[6],[9]]
In [169]: alist
Out[169]: [[0], [3], [6], [9]]
In [170]: alist[1]
Out[170]: [3]
In [171]: alist[1][0]
Out[171]: 3
I can index it twice.
In Octave, the poor man's MATLAB
>> x = [0,3,6,9];
>> x(2)
ans = 3
>> size(x)
ans =
1 4
>> size(x(2))
ans =
1 1
x(2) is still a 2d matrix; I could index it indefinitely, x(2)(1)(1)(1). Size itself is a 2d matrix; everything in MATLAB is 2d (or higher).
>> size(size(x))
ans =
1 2
Back in Python/numpy:
In [172]: arr = np.array([0,3,6,9])
In [173]: arr.shape
Out[173]: (4,) # a 1 element tuple
In [175]: arr[1]
Out[175]: 3
In [176]: type(Out[175])
Out[176]: numpy.int64
In [177]: Out[175].shape
Out[177]: ()
The result of indexing an element of that 1d array is a numpy scalar object, with a 0d shape. https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
Judging from many questions, it seems that MATLAB users have trouble conceiving of an array with 1 or even 0 dimensions. That lower 2d bound becomes thoroughly ingrained in their thinking. It also seems to be foundational to some (if not all) versions of linear algebra. There are matrices, and row vectors and column vectors, but not 'plain' vectors.
But numpy runs in Python, and the behavior of its arrays is consistent with Python lists. And logically consistent with itself.
Here's what a 'column' vector and 'row' vector look like. Note the shapes - both 2 element tuples. And nesting of brackets (2 levels). The similarity to nested list is intentional.
In [178]: arr = np.array([[0],[3],[6],[9]])
In [179]: arr.shape
Out[179]: (4, 1)
In [180]: arr
Out[180]:
array([[0],
[3],
[6],
[9]])
In [181]: arr = np.array([[0,3,6,9]])
In [182]: arr.shape
Out[182]: (1, 4)
In [183]: arr
Out[183]: array([[0, 3, 6, 9]])
Step back from numpy a moment and look at Python lists:
In [165]: alist = [0,3,6,9]
In [166]: alist
Out[166]: [0, 3, 6, 9]
In [167]: alist[1]
Out[167]: 3
This 3 is a scalar; I'd get an error if I tried index it, alist[1][0].
Now make a list of lists:
In [168]: alist = [[0],[3],[6],[9]]
In [169]: alist
Out[169]: [[0], [3], [6], [9]]
In [170]: alist[1]
Out[170]: [3]
In [171]: alist[1][0]
Out[171]: 3
I can index it twice.
In Octave, the poor man's MATLAB
>> x = [0,3,6,9];
>> x(2)
ans = 3
>> size(x)
ans =
1 4
>> size(x(2))
ans =
1 1
x(2) is still a 2d matrix; I could index it indefinitely, x(2)(1)(1)(1). Size itself is a 2d matrix; everything in MATLAB is 2d (or higher).
>> size(size(x))
ans =
1 2
Back in Python/numpy:
In [172]: arr = np.array([0,3,6,9])
In [173]: arr.shape
Out[173]: (4,) # a 1 element tuple
In [175]: arr[1]
Out[175]: 3
In [176]: type(Out[175])
Out[176]: numpy.int64
In [177]: Out[175].shape
Out[177]: ()
The result of indexing an element of that 1d array is a numpy scalar object, with a 0d shape. https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
Judging from many questions, it seems that MATLAB users have trouble conceiving of an array with 1 or even 0 dimensions. That lower 2d bound becomes thoroughly ingrained in their thinking. It also seems to be foundational to some (if not all) versions of linear algebra. There are matrices, and row vectors and column vectors, but not 'plain' vectors.
But numpy runs in Python, and the behavior of its arrays is consistent with Python lists. And logically consistent with itself.
Here's what a 'column' vector and 'row' vector look like. Note the shapes - both 2 element tuples. And nesting of brackets (2 levels). The similarity to nested list is intentional.
In [178]: arr = np.array([[0],[3],[6],[9]])
In [179]: arr.shape
Out[179]: (4, 1)
In [180]: arr
Out[180]:
array([[0],
[3],
[6],
[9]])
In [181]: arr = np.array([[0,3,6,9]])
In [182]: arr.shape
Out[182]: (1, 4)
In [183]: arr
Out[183]: array([[0, 3, 6, 9]])
edited Mar 24 at 5:40
answered Mar 24 at 5:27
hpauljhpaulj
121k789164
121k789164
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55319224%2fnumpy-reshape-question-matlab-comparison%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
numpy has real 1d arrays,
– hpaulj
Mar 23 at 23:21
1
rows and columns are convenient descriptors for 2d arrays. They are not formally defined or used. numpy always talks about axes.
– hpaulj
Mar 24 at 0:19
That
-1in reshape is 'what ever works'. MATLAB uses [] for that.– hpaulj
Mar 24 at 1:15