Home > vbmeg > external > mne > fiff_write_float_sparse_rcs.m

fiff_write_float_sparse_rcs

PURPOSE ^

SYNOPSIS ^

function fiff_write_float_sparse_rcs(fid,kind,mat)

DESCRIPTION ^

 fiff_write_float_sparsce_rcs(fid,kind,mat)
 
 Writes a single-precision sparse (RCS) floating-point matrix tag

     fid           An open fif file descriptor
     kind          The tag kind
     mat           The data matrix

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function fiff_write_float_sparse_rcs(fid,kind,mat)
0002 %
0003 % fiff_write_float_sparsce_rcs(fid,kind,mat)
0004 %
0005 % Writes a single-precision sparse (RCS) floating-point matrix tag
0006 %
0007 %     fid           An open fif file descriptor
0008 %     kind          The tag kind
0009 %     mat           The data matrix
0010 %
0011 
0012 %
0013 %   Author : Matti Hamalainen, MGH Martinos Center
0014 %   License : BSD 3-clause
0015 %
0016 
0017 me='MNE:fiff_write_float_sparse_rcs';
0018 
0019 if nargin ~= 3
0020    error(me,'Incorrect number of arguments');
0021 end
0022 
0023 if ~issparse(mat)
0024    error(me,'Input should be a sparse matrix');
0025 end
0026 
0027 if length(size(mat)) ~= 2
0028    error(me,'Input should be a two-dimensional matrix');
0029 end
0030 
0031 FIFFT_FLOAT  = 4;
0032 FIFFT_MATRIX = bitshift(16416,16);    % 4020
0033 FIFFT_MATRIX_FLOAT_RCS = bitor(FIFFT_FLOAT,FIFFT_MATRIX);
0034 FIFFV_NEXT_SEQ=0;
0035 %
0036 %    nnz values
0037 %    nnz column indices
0038 %    nrow+1 pointers
0039 %    dims
0040 %    nnz
0041 %    ndim
0042 %
0043 nnzm = nnz(mat);
0044 nrow = size(mat,1);
0045 datasize = 4*nnzm + 4*nnzm + 4*(nrow+1) + 4*4;
0046 %
0047 %    Nonzero entries
0048 %
0049 [ s(:,1), s(:,2), s(:,3) ] = find(mat);
0050 s = sortrows(s,1);
0051 [ rows, starts ] = unique(s(:,1),'first');
0052 
0053 count = fwrite(fid,int32(kind),'int32');
0054 if count ~= 1
0055    error(me,'write failed');
0056 end
0057 count = fwrite(fid,int32(FIFFT_MATRIX_FLOAT_RCS),'int32');
0058 if count ~= 1
0059    error(me,'write failed');
0060 end
0061 count = fwrite(fid,int32(datasize),'int32');
0062 if count ~= 1
0063    error(me,'write failed');
0064 end
0065 count = fwrite(fid,int32(FIFFV_NEXT_SEQ),'int32');
0066 if count ~= 1
0067    error(me,'write failed');
0068 end
0069 %
0070 %   The data values
0071 %
0072 count = fwrite(fid,single(s(:,3)),'single');
0073 if count ~= nnzm
0074    error(me,'write failed');
0075 end
0076 %
0077 %   Column indices
0078 %
0079 count = fwrite(fid,int32(s(:,2)-1),'int32');
0080 if count ~= nnzm
0081    error(me,'write failed');
0082 end
0083 %
0084 %   Pointers
0085 %
0086 ptrs = -ones(1,nrow+1);
0087 for k = 1:length(rows)
0088    ptrs(rows(k)) = starts(k) - 1;
0089 end
0090 ptrs(nrow+1) = nnzm;
0091 %
0092 %   Fill in pointers for empty rows
0093 %
0094 for k = nrow:-1:1
0095    if ptrs(k) < 0
0096       ptrs(k) = ptrs(k+1);
0097    end
0098 end
0099 %
0100 count = fwrite(fid,int32(ptrs),'int32');
0101 if count ~= nrow+1
0102    error(me,'write failed');
0103 end
0104 %
0105 %   Dimensions
0106 %
0107 dims(1) = nnz(mat);
0108 dims(2) = size(mat,1);
0109 dims(3) = size(mat,2);
0110 dims(4) = 2;
0111 count = fwrite(fid,int32(dims),'int32');
0112 if count ~= 4
0113    error(me,'write failed');
0114 end
0115 
0116 return;
0117

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005