function y = unspar(x, realFlag) % unspar -- Convert "spar" array to "sparse". % unspar(x) converts the "spar" array x % to a Matlab "sparse" array, if the data % represent a two-dimensional array; otherwise, % to a "full" array. See "help spar". % unspar(x, realFlag) assumes that x lacks the % imaginary-component column, if realFlag is TRUE. % NOTE: a complex-array will be auto-detected, so % long as the next-to-last column contains values % that would be invalid as indices. % Copyright (C) 1999 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 19-Aug-1999 14:30:00. % Updated 24-Aug-1999 11:34:47. if nargin < 1, help(mfilename), return, end if nargin < 2, realFlag = 0; end [m, n] = size(x); if realFlag valid_indices =... isfinite(x(:, end-1)) & ... (x(:, end-1) > 0) & ... (x(:, end-1) == fix(x(:, end-1))); if ~all(valid_indices), realFlag = 0; end end if ~realFlag dim = n-2; else dim = n-1; end siz = zeros(1, dim); sub = cell(1, dim); for j = 1:dim dat = x(:, j); siz(j) = max(dat); sub{j} = dat; end ind = sub2ind(siz, sub{:}); if dim == 2 if ~realFlag result = sparse(sub{1}, sub{2}, x(:, end-1) + sqrt(-1) * x(:, end)); else result = sparse(sub{1}, sub{2}, x(:, end)); end else result = zeros(siz); if ~realFlag result(ind) = x(:, end-1) + sqrt(-1) * x(:, end); else result(ind) = x(:, end); end end if nargout > 0 y = result; else disp(result) assignin('caller', 'ans', result) end