storage format using SPARSE and INT8

조회 수: 16 (최근 30일)
Giovanni Gardan
Giovanni Gardan 2020년 5월 27일
댓글: Steven Lord 2024년 12월 11일 16:23
I'd like to save storage and time when I run my code. In order to do this I'm trying to modify the format-storage of some (1 0 -1) matrixes and in the follow example I note that int8 is the best. That's ok!
But if I try to save B as a sparse matrix (see the comment) I get the following error:
Undefined function 'sparse' for input arguments of type 'int8'.
1) So, is it possible to save a sparse matrix (made only with the values of element 1, 0 or -1) not with double precision, but with int storage-format to save too much storage?
2) In general, does making operation between different saved elements (for example the sum of matrix B with another matrix, but saved in double precision) create problems??
%Code.m
A = [1 0 0 0 0 0 0 0 0 0 0 0 0;
-1 0 0 0 0 0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 0 0 1 0 0 0];
B= int8(A);
C = sparse(A);
% D=sparse(B) <<<<<<<<<HERE THE COMMENT
whos A B C
-------------------------------------------------------------------------------------------------
%Command window shows
>> Code
Name Size Bytes Class Attributes
A 3x13 312 double
B 3x13 39 int8
C 3x13 176 double sparse

답변 (1개)

Shlok
Shlok 2024년 12월 11일 12:57
Hi Giovanni,
I understand that you want to optimize storage and runtime by using int8 for matrices containing only the values 1, 0, and –1.
Unfortunately, MATLAB's sparse function does not support int8 or other integer types directly. The sparse function is designed to work with double precision by default. Sparse matrices store both the values and their indices, resulting in significant overhead.
For a single entry in a sparse matrix:
Double precision (default):
Value (64 bits) + Row index (32 bits) + Column index (32 bits) = 128 bits (16 bytes)
Int8 (assuming, if supported):
Value (8 bits) + Row index (32 bits) + Column index (32 bits) = 72 bits (9 bytes)
Savings Calculation:
However, due to memory alignment, indexing structures, and implementation overhead, the actual memory savings in practice are closer to 10–20%.
Modern 64-bit memory architecture, which often packs data in chunks, further reduces potential savings. Therefore, sparse function just stores values using double precision by default.
MATLAB automatically typecasts the lower priority item to the higher priority type when performing operations between matrices of different storage formats (e.g., int8 and double).
For example, if you add an int8 matrix to a double matrix, the int8 matrix will be converted to double before the operation. This ensures that the calculation is performed correctly, but it can negate the storage benefits of using integer types.
Refer to the following MATLAB Answer link to know more about memory usage in a sparse matrix:
  댓글 수: 1
Steven Lord
Steven Lord 2024년 12월 11일 16:23
You are able to create sparse double or sparse logical matrices in MATLAB. Years ago (prior to release R13 IIRC) sparse was a class and logical was an attribute, meaning you could have a sparse (logical) matrix. When we changed sparse to be an attribute (like complex) and logical a class, those matrices became logical (sparse) matrices.
SD = speye(3);
SL = SD > 0.5;
FD = eye(3);
FL = FD > 0.5;
FCD = FD*(1+1i);
SCD = sparse(FCD);
whos SD SL FD FL FCD SCD
Name Size Bytes Class Attributes FCD 3x3 144 double complex FD 3x3 72 double FL 3x3 9 logical SCD 3x3 104 double sparse, complex SD 3x3 80 double sparse SL 3x3 59 logical sparse
Way back when, SL would have been listed as having Class sparse and Attributes logical.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by