Cody

# Problem 691. Number of bytes required to store a sparse matrix

Solution 703680

Submitted on 20 Jul 2015 by Peng Liu
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% x = sparse(100,1000,0.01); y_correct = 4016; assert(isequal(your_fcn_name(x),y_correct))

2   Pass
%% x = sparse(10000,10000,pi); y_correct = 40016 assert(isequal(your_fcn_name(x),y_correct))

y_correct = 40016

3   Pass
%% x = sparse(10,15); y_correct = 76 assert(isequal(your_fcn_name(x),y_correct))

y_correct = 76

4   Pass
%% B = [ 41 11 0 52 22 0 63 33 13 74 44 24 ]; d = [-3 0 2]; x = spdiags(B,d,7,4) y_correct = 140 assert(isequal(your_fcn_name(x),y_correct))

x = (1,1) 11 (4,1) 41 (2,2) 22 (5,2) 52 (1,3) 13 (3,3) 33 (6,3) 63 (2,4) 24 (4,4) 44 (7,4) 74 y_correct = 140

5   Pass
%% Random sparse matrix (harder) %x=sprand(100,1000,0.01); %z = whos('x') %y_correct = round(z.bytes) %your_sol = round(your_fcn_name(x)) %assert(isequal(round(your_fcn_name(x)),y_correct))

6   Pass
%% Prevents cheating filetext = fileread('your_fcn_name.m') assert(isempty(strfind(filetext, 'whos'))) assert(isempty(strfind(filetext, 'who')))

filetext = function ans = your_fcn_name(x) % The minimum data storage requirement formula for a double m x n sparse % matrix with nnz non-zero elements, including the index data, is as % follows on a 32-bit system: % % max(nnz(x),1) * (4 + 8) + (size(x,2)+1)*4; % 32-bit % max(nnz(x),1) * (8 + 8) + (size(x,2)+1)*8; % 64-bit % % nnz * 4 = Storing the row index of the non-zero elements % nnz * 8 = Storing the non-zero double element values themselves % (n+1)*4 = Storing the cumulative number of non-zero elements thru column % % For 64-bit systems using 8-byte integers for the indexing you can replace % the 4's above with 8's. nzmax(x) * 12 + size(x,2)*4 + 4; % 32-bit % max(nnz(x),1) * 12 + (size(x,2)+1)*4; % 32-bit %This code written by profile_id 2951682