MATLAB中如何知道变量的大小

发布于 2024-10-14 18:30:30 字数 158 浏览 2 评论 0原文

我在 MATLAB 中有变量,我已经使用 class() 检查了它们的类,但我也想知道它们在内存中占用的大小。更准确地说,我知道它们是 double 类型,并且我想确保它们是 32 位 double 而不是 64 位。

我使用的MATLAB版本是R2009b。

I have variables in MATLAB, I've checked their class using class() but I also want to know the size that they take in the memory. More accurately, I know that they are of double type, and I want to make sure that they are 32-bit double and not 64-bit.

The version of the MATLAB I'm using is R2009b.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雄赳赳气昂昂 2024-10-21 18:30:30

我编写了一个简单的便利函数来处理这个问题。用法是:

>> x = ones(1000);
>> ByteSize(x)
7.63 Mb

我运行的是R2007a,所以Java对象不返回大小的问题可能已经在后续版本中得到修复。这是代码:

function ByteSize(in, fid)
% BYTESIZE writes the memory usage of the provide variable to the given file
% identifier. Output is written to screen if fid is 1, empty or not provided.

if nargin == 1 || isempty(fid)
    fid = 1;
end

s = whos('in');
fprintf(fid,[Bytes2str(s.bytes) '\n']);
end

function str = Bytes2str(NumBytes)
% BYTES2STR Private function to take integer bytes and convert it to
% scale-appropriate size.

scale = floor(log(NumBytes)/log(1024));
switch scale
    case 0
        str = [sprintf('%.0f',NumBytes) ' b'];
    case 1
        str = [sprintf('%.2f',NumBytes/(1024)) ' kb'];
    case 2
        str = [sprintf('%.2f',NumBytes/(1024^2)) ' Mb'];
    case 3
        str = [sprintf('%.2f',NumBytes/(1024^3)) ' Gb'];
    case 4
        str = [sprintf('%.2f',NumBytes/(1024^4)) ' Tb'];
    case -inf
        % Size occasionally returned as zero (eg some Java objects).
        str = 'Not Available';
    otherwise
       str = 'Over a petabyte!!!';
end
end

I wrote a simple convenience function to handle just this problem. Usage is:

>> x = ones(1000);
>> ByteSize(x)
7.63 Mb

I run R2007a, so the problem of Java objects not returning sizes may have been fixed in subsequent releases. Here's the code:

function ByteSize(in, fid)
% BYTESIZE writes the memory usage of the provide variable to the given file
% identifier. Output is written to screen if fid is 1, empty or not provided.

if nargin == 1 || isempty(fid)
    fid = 1;
end

s = whos('in');
fprintf(fid,[Bytes2str(s.bytes) '\n']);
end

function str = Bytes2str(NumBytes)
% BYTES2STR Private function to take integer bytes and convert it to
% scale-appropriate size.

scale = floor(log(NumBytes)/log(1024));
switch scale
    case 0
        str = [sprintf('%.0f',NumBytes) ' b'];
    case 1
        str = [sprintf('%.2f',NumBytes/(1024)) ' kb'];
    case 2
        str = [sprintf('%.2f',NumBytes/(1024^2)) ' Mb'];
    case 3
        str = [sprintf('%.2f',NumBytes/(1024^3)) ' Gb'];
    case 4
        str = [sprintf('%.2f',NumBytes/(1024^4)) ' Tb'];
    case -inf
        % Size occasionally returned as zero (eg some Java objects).
        str = 'Not Available';
    otherwise
       str = 'Over a petabyte!!!';
end
end
揪着可爱 2024-10-21 18:30:30

您可以使用 whos 来获取描述等的结构数组things,每个变量的大小(以字节为单位)。

请注意,根据定义,双精度数是 64 位!

You can use whos to obtain an array of structures that describe, amongst other things, the size in bytes of each variable.

Note that a double is, by definition, 64-bit!

蒲公英的约定 2024-10-21 18:30:30
dt=whos('VARIABLE_YOU_CARE_ABOUT'); MB=dt.bytes*9.53674e-7;  

这将为您提供以兆字节为单位的大小,例如 MB=123.78

dt=whos('VARIABLE_YOU_CARE_ABOUT'); MB=dt.bytes*9.53674e-7;  

This will give you the size in megabytes, for example MB=123.78

深海夜未眠 2024-10-21 18:30:30

我尝试增强“MatlabSorter”的简单功能来处理这个问题。用法仍然相同:

>> x = ones(1000);
>> getByteSize(x)
7.63 mb

补充:

1.您可以说明您寻求哪种类型的返回 - b、kb、mb、tb 或 pb

2.您可以将结果作为变量获取,而无需将其打印在屏幕上

这是代码:

function b = getByteSize(theVariable, returnType, fid)
% getByteSize returns the mem.usage of the provided variable(theVariable) to the given file
% identifier. 
% returnType is assigned meaningfully according to the byte size if not stated
% Output is written to screen if fid is 1, empty or not provided.
s = whos('theVariable');
b = s.bytes;
if nargin == 1 || isempty(returnType)
    scale = floor(log(b)/log(1024));
    switch scale
        case 0
            returnType = 'byte';
        case 1
            returnType = 'kb';
        case 2
            returnType = 'mb';
        case 3
            returnType = 'gb';
        case 4
            returnType = 'tb';
        case -inf
            % Size occasionally returned as zero (eg some Java objects).
            returnType = 'byte';
            warning('Size occasionally returned as zero (eg some Java objects). Bytes assumed');
        otherwise
            returnType = 'petabytes';
            warning('Over 1024 petabyte. petabytes assumed');
    end
end
switch returnType
    case {'b','byte','bytes'}
        b = s.bytes;
    case {'kb','kbs','kilobyte','kilobytes'}
        b = b / 1024;
    case {'mb','mbs','megabyte','megabytes'}
        b = b / 1024^2;
    case {'gb','gbs','gigabyte','gigabytes'}
        b = b / 1024^3;
    case {'tb','tbs','terabyte','terabytes'}
        b = b / 1024^4;
    case {'pb','pbs','petabyte','petabytes'}
        b = b / 1024^5;
    otherwise
        returnType = 'bytes';
end
if nargin <= 2 || isempty(fid) || fid == 1
    fprintf(1,[num2str(b) ' ' returnType '\n']);
elseif nargin > 2 && ~isempty(fid) && fid > 2
    try
        fprintf(fid,[num2str(b) ' ' returnType '\n']);
    catch
        warning(['fid(' num2str(fid) ') could not be edited. Hence the output will be written on the screen.']);
        fprintf(1,[num2str(b) ' ' returnType '\n']);
    end
end
end

I tried to enhance ' MatlabSorter's ' simple function to handle this problem. Usage is still the same:

>> x = ones(1000);
>> getByteSize(x)
7.63 mb

additions :

1.you can state which type of return you seek for - b, kb, mb, tb or pb

2.you can get the result as a variable without printing it on the screen

Here's the code:

function b = getByteSize(theVariable, returnType, fid)
% getByteSize returns the mem.usage of the provided variable(theVariable) to the given file
% identifier. 
% returnType is assigned meaningfully according to the byte size if not stated
% Output is written to screen if fid is 1, empty or not provided.
s = whos('theVariable');
b = s.bytes;
if nargin == 1 || isempty(returnType)
    scale = floor(log(b)/log(1024));
    switch scale
        case 0
            returnType = 'byte';
        case 1
            returnType = 'kb';
        case 2
            returnType = 'mb';
        case 3
            returnType = 'gb';
        case 4
            returnType = 'tb';
        case -inf
            % Size occasionally returned as zero (eg some Java objects).
            returnType = 'byte';
            warning('Size occasionally returned as zero (eg some Java objects). Bytes assumed');
        otherwise
            returnType = 'petabytes';
            warning('Over 1024 petabyte. petabytes assumed');
    end
end
switch returnType
    case {'b','byte','bytes'}
        b = s.bytes;
    case {'kb','kbs','kilobyte','kilobytes'}
        b = b / 1024;
    case {'mb','mbs','megabyte','megabytes'}
        b = b / 1024^2;
    case {'gb','gbs','gigabyte','gigabytes'}
        b = b / 1024^3;
    case {'tb','tbs','terabyte','terabytes'}
        b = b / 1024^4;
    case {'pb','pbs','petabyte','petabytes'}
        b = b / 1024^5;
    otherwise
        returnType = 'bytes';
end
if nargin <= 2 || isempty(fid) || fid == 1
    fprintf(1,[num2str(b) ' ' returnType '\n']);
elseif nargin > 2 && ~isempty(fid) && fid > 2
    try
        fprintf(fid,[num2str(b) ' ' returnType '\n']);
    catch
        warning(['fid(' num2str(fid) ') could not be edited. Hence the output will be written on the screen.']);
        fprintf(1,[num2str(b) ' ' returnType '\n']);
    end
end
end
影子是时光的心 2024-10-21 18:30:30

另一种使用 whos 函数的班轮

x=1;
Nbytes=getfield(whos('x'),'bytes')

Yet another one liner using whos function

x=1;
Nbytes=getfield(whos('x'),'bytes')
音盲 2024-10-21 18:30:30

您还可以显示工作区本身中变量的字节大小。

  1. 找到工作区。
  2. 单击工作区标题栏中的箭头图标。
  3. 将鼠标悬停在“选择列”上。
  4. 单击“字节”。

屏幕截图MATLAB 中的工作区面板,其中“字节”列可见,并显示可在其中切换该列的下拉菜单。

You can also show the byte size of variables in the workspace itself.

  1. Find the workspace.
  2. Click the arrow icon in the workspace's title bar.
  3. Hover over "Choose Columns".
  4. Click "Bytes".

Screenshot of the workspace panel in MATLAB, with the Bytes column visible, and showing the dropdown menu in which the column can be toggled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文