MATLAB Answers

Sizeof double float int etc

17 ビュー (過去 30 日間)
R. Can
R. Can 2021 年 6 月 30 日
回答済み: Steven Lord 2021 年 6 月 30 日
Hello there,
I need to know how to find an equivallent function in Matlab to the sizeof function in c++.
For example, in c++ if I write sizeof(double) I would get the amount of memory needed to store a double.
I need something very similar with a matrix now. I will be storing bigger and bigger matrix and I need to find their size in the memory.
Could someone of you please help me?
all best,
:)
  1 件のコメント
Yongjian Feng
Yongjian Feng 2021 年 6 月 30 日
Did you try
whos

サインインしてコメントする。

採用された回答

Jan
Jan 2021 年 6 月 30 日
編集済み: Jan 2021 年 6 月 30 日
Version 1:
w = whos('z');
nbytes = w.bytes;
Version 2:
function S = sizeof(X)
switch class(X)
case {'uint64', 'int64', 'double'}
m = 8;
case {'uint32', 'int32', 'single'}
m = 4;
case {'uint16', 'int16', 'char'}
m = 2;
case {'uint8', 'int8', 'logical'}
m = 1;
otherwise
error('Jan:sizeof:Type', 'Type not handles: %s', class(X));
end
S = m * numel(X);
end
A search inthe forum leads me to: https://www.mathworks.com/matlabcentral/answers/391214-does-matlab-have-a-function-which-is-similar-with-sizeof-in-c#answer_312420 . Obviously I have some standard programming styles...

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2021 年 6 月 30 日
Look at the .bytes returned by whos()
a=false(5);
aInfo=whos('a')
b=zeros(5);
bInfo=whos('b')

Steven Lord
Steven Lord 2021 年 6 月 30 日
I need something very similar with a matrix now. I will be storing bigger and bigger matrix and I need to find their size in the memory.
Why? If you're trying to create an array to be filled in, just call a function like ones or zeros and tell it the size of the array you want to create (in terms of number of elements) and optionally the type. There's no need for you to know (or care) how much memory each element takes up in order to build the array.
A = ones(2, 3, 'int16')
A = 2×3
1 1 1 1 1 1
B = zeros(5) % 5-by-5 double array
B = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by