Notice that |ones(10^6)| creates a square matrix with 1e6*1e6 = 1e12 elements, which needs a free memory block of 8 TB. So with less than 16 TB free RAM I would not expect this to run reliably. Answer from Jan on mathworks.com
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
size - Array size - MATLAB
You can specify dim as a vector of positive integers to query multiple dimension lengths at a time. Alternatively, you can list the queried dimensions as separate input arguments dim1,dim2,...,dimN. For an example, see Size of 4-D Array. length | strlength | ndims | numel | height | width · You clicked a link that corresponds to this MATLAB command:
🌐
MathWorks
mathworks.com › instrument control toolbox › driver-based instrument communication › generic instrument drivers
size - Size of instrument object array - MATLAB
m = size(obj,dim) returns the length of the dimension specified by the scalar dim. For example, size(obj,1) returns the number of rows. ... Run the command by entering it in the MATLAB Command Window.
🌐
MathWorks
mathworks.com › matlab › programming › classes › define classes › class hierarchies › subclass applications
Use of size and numel with Classes - MATLAB & Simulink
The size and numel functions work consistently with arrays of user-defined objects. There is generally no need to overload size or numel in user-defined classes. Several MATLAB® functions use size and numel to perform their operations.
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
length - Length of largest array dimension - MATLAB
To examine the dimensions of a table, use the height, width, or size functions. ... The length function fully supports tall arrays. For more information, see Tall Arrays. The length function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Top answer
1 of 4
5
Do you mean something like this: function GetSize(this) props = properties(this); totSize = 0; for ii=1:length(props) currentProperty = getfield(this, char(props(ii))); s = whos('currentProperty'); totSize = totSize + s.bytes; end fprintf(1, '%d bytes\n', totSize); end
2 of 4
3
I extended the code of Dmitry and Mario, such that it now also treats nested objects properly, and it pretty-prints the result. You can also put a size treshold, to only show significantly large fields. Example output: >> getMemSize(GPS, 1024^2, 'GPS') [GPS] usedgridpoints : 3 Mb phi : 24 Mb Vtrapx : 24 Mb Vtrapy : 24 Mb Vtrapz : 24 Mb FLaplacian : 24 Mb FUdd : 24 Mb FFTshift_rphase : 24 Mb FFTshift_kphase : 24 Mb [BdGbasis3Dsymm] BasisStates : 8 Gb [!] PhiBuffer : 24 Mb L : 76 Mb SourceIndices : 16 Mb TargetIndices : 16 Mb TOTAL : 9 Gb [!] Code: function [ bytes ] = getMemSize( variable, sizelimit, name, indent ) if nargin < 2 sizelimit = -1; end if nargin < 3 name = 'variable'; end if nargin < 4 indent = ''; end strsize = 30; props = properties(variable); if size(props, 1) < 1 bytes = whos(varname(variable)); bytes = bytes.bytes; if bytes > sizelimit if bytes < 1024 fprintf('%s%s: %i\n', indent, pad(name, strsize - length(indent)), bytes); elseif bytes < 2^20 fprintf('%s%s: %i Kb\n', indent, pad(name, strsize - length(indent)), round(bytes / 2^10)); elseif bytes < 2^30 fprintf('%s%s: %i Mb\n', indent, pad(name, strsize - length(indent)), round(bytes / 2^20)); else fprintf('%s%s: %i Gb [!]\n', indent, pad(name, strsize - length(indent)), round(bytes / 2^30)); end end else fprintf('\n%s[%s] \n\n', indent, name); bytes = 0; for ii=1:length(props) currentProperty = getfield(variable, char(props(ii))); pp = props(ii); bytes = bytes + getMemSize(currentProperty, sizelimit, pp{1}, [indent, ' ']); end if length(indent) == 0 fprintf('\n'); name = 'TOTAL'; if bytes < 1024 fprintf('%s%s: %i\n', indent, pad(name, strsize - length(indent)), bytes); elseif bytes < 2^20 fprintf('%s%s: %i Kb\n', indent, pad(name, strsize - length(indent)), round(bytes / 2^10)); elseif bytes < 2^30 fprintf('%s%s: %i Mb\n', indent, pad(name, strsize - length(indent)), round(bytes / 2^20)); else fprintf('%s%s: %i Gb [!]\n', indent, pad(name, strsize - length(indent)), round(bytes / 2^30)); end end end end
Find elsewhere
🌐
YouTube
youtube.com › watch
How to find the size of a matrix in matlab | size of a matrix in matlab - YouTube
In this tutorial you will learnhow to find the size of a matrix in matlab,size of matrix in matlab,how to get the number of rows in matrix in matlab,how to g...
Published: September 10, 2020
🌐
Quora
quora.com › What-is-the-difference-between-length-and-size-in-MATLAB
What is the difference between length and size in MATLAB? - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
Top answer
1 of 2
4

.shape() returns a tuple. Since we can check the length of tuples, we can just define a custom size() function that inserts a 1 if the array is 1D:

def size(arr):
    if len(arr.shape) == 1:
        return arr.shape[0], 1
    return arr.shape
2 of 2
0

shape IS the numpy equivalent.

In [127]: alpha = np.array([20, 30, 40, 45, 50, 60])
In [128]: alpha.shape
Out[128]: (6,)

MATLAB matrices are always 2d (or higher), so size will have 2 values. But numpy arrays can be 1d, or even 0d. So shape may just have 1 value as in (6,).

Sure you could construct mm,nn with these values:

In [129]: mm, nn = 6, 1

and even iterate on the ranges, but that doesn't help you access the elements of alpha:

In [130]: for i in range(nn):
     ...:     print(i, alpha[i])
     ...:     for ii in range(mm):
     ...:         print(ii)
     ...:         print(alpha[i, ii])
0 20
0
Traceback (most recent call last):
  Input In [130] in <module>
    print(alpha[i, ii])
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

alpha[0] is 20, but alpha[0,0] raises this error. alpha is 1d.

There are various ways of making a 2d array:

In [132]: beta = np.atleast_2d(alpha)
In [133]: beta
Out[133]: array([[20, 30, 40, 45, 50, 60]])
In [134]: beta.shape
Out[134]: (1, 6)
In [135]: beta[0, 3]
Out[135]: 45

or adding a trailing dimension:

In [136]: alpha[:, None].shape
Out[136]: (6, 1)

Sometimes we call these arrays "row vector" or "column vector", but in either case they are 2d arrays;

In [137]: beta
Out[137]: array([[20, 30, 40, 45, 50, 60]])
In [138]: alpha[:, None]
Out[138]: 
array([[20],
       [30],
       [40],
       [45],
       [50],
       [60]])

Sooner of later you need to become comfortable with the multiple-dimensions of numpy. Trying stick with the MATLAB notions will, in the long run, be frustrating. There are some older helps for way-ward MATLAB users, such as the np.matrix class, and https://numpy.org/doc/stable/user/numpy-for-matlab-users.html

In python it is possible to iterate on lists and arrays directly, and use enumerate if you also want an index:

In [140]: for i, v in enumerate(alpha):
     ...:     print(i, v, alpha[i])
0 20 20
1 30 30
2 40 40
3 45 45
4 50 50
5 60 60

But numpy we prefer not to iterate - not even with one level. And often we don't need to. (Same is/was true in MATLAB. Whole matrix opertions are preferable, though its jit compiling reduces the time penalty of iteration.)

In [141]: np.arange(alpha.shape[0])
Out[141]: array([0, 1, 2, 3, 4, 5])
In [142]: alpha
Out[142]: array([20, 30, 40, 45, 50, 60])
In [143]: alpha * np.arange(alpha.shape[0])
Out[143]: array([  0,  30,  80, 135, 200, 300])
🌐
MathWorks
mathworks.com › matlab › language fundamentals › matrices and arrays
ndims - Number of array dimensions - MATLAB
In other words, ndims(A) = length(size(A)). ... The ndims function fully supports tall arrays. For more information, see Tall Arrays. The ndims function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
🌐
Northwestern University
ece.northwestern.edu › local-apps › matlabhelp › techdoc › ref › size.html
size (MATLAB Functions)
[d1,d2,d3,...,dn] = size(X) returns the sizes of the first n dimensions of array X in separate variables.
🌐
MathWorks
mathworks.com › statistics and machine learning toolbox › descriptive statistics and visualization › managing data
dataset.size - (Not Recommended) Size of dataset array - MATLAB
M = size(A,dim) returns the length of the dimension specified by the scalar dim: ... Run the command by entering it in the MATLAB Command Window.
🌐
MathWorks
mathworks.com › mapping toolbox › data analysis › vector data
size - Return size of geographic or planar vector - MATLAB
Size of vector v in the second dimension, returned as the value 1. ... Run the command by entering it in the MATLAB Command Window.
🌐
MathWorks
mathworks.com › system identification toolbox › data preparation › represent data
size - Determine size of iddata data set - MATLAB
[ns,ny,nu,ne] = size(data) returns, for the iddata object data, the number of data samples ns in each experiment, the number of outputs ny, the number of inputs nu, and the number of experiments ne.