0001 function fiff_write_float_sparse_rcs(fid,kind,mat)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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);
0033 FIFFT_MATRIX_FLOAT_RCS = bitor(FIFFT_FLOAT,FIFFT_MATRIX);
0034 FIFFV_NEXT_SEQ=0;
0035
0036
0037
0038
0039
0040
0041
0042
0043 nnzm = nnz(mat);
0044 nrow = size(mat,1);
0045 datasize = 4*nnzm + 4*nnzm + 4*(nrow+1) + 4*4;
0046
0047
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
0071
0072 count = fwrite(fid,single(s(:,3)),'single');
0073 if count ~= nnzm
0074 error(me,'write failed');
0075 end
0076
0077
0078
0079 count = fwrite(fid,int32(s(:,2)-1),'int32');
0080 if count ~= nnzm
0081 error(me,'write failed');
0082 end
0083
0084
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
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
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