-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray2SpArray.m
33 lines (30 loc) · 1.09 KB
/
array2SpArray.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function spA = array2SpArray(A)
%ARRAY2SPARRAY Convert a full array into a sparse array structure.
%
% spA = array2spArray(A) A sparse array structure has the following fields:
%
% 'Size', which is a row vector of the sizes of each dimension of the full
% array -- for example, a column vector with N entries has a size of N; a row
% vector with N entries has a size of (1,N); a matrix with N entries has a
% size (J,K), where JK = N; a three-way array with N entries has a size
% (J,K,L), where JKL = N;
%
% 'Ind', which is a column vector of linear indices of nonzero values in the
% full array that they represent;
%
% 'Val', which is a column vector of the values at those indices.
%
% All singleton dimensions are removed.
%
% By Andrew J. Milne, The MARCS Institute, Western Sydney University
%
% See also SPARRAY2ARRAY.
[indA,~,valA] = find(A(:)); % find nonzeros in full array
sizA = size(A); % get size of full array
sizA(sizA==1) = []; % remove all singleton dimensions
if isempty(sizA)
sizA = [];
end
% Create structure
spA = struct('Size',sizA,'Ind',indA,'Val',valA);
end