-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec.m
More file actions
42 lines (36 loc) · 934 Bytes
/
vec.m
File metadata and controls
42 lines (36 loc) · 934 Bytes
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
34
35
36
37
38
39
40
41
42
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%VEC vectorize array
%%function B = vec(A, K)
%%
%%INPUT:
%% A - array of any dimension
%% K - vector dimension (default 1 for column)
%%
%%OUTPUT:
%% B - vectorized array
%%
%%EXAMPLES:
%% A = (1:4);
%% B = vec(A); % column
%% B = vec(A, 1); % column (redundant)
%% B = vec(A, 2); % row
%% B = vec(A, 3); % vector in 3rd dimension
%% B = vec(A, 0); % error
%% B = vec(A, -1); % error
%% B = vec(A, 1.5); % error
%%
%%DESCRIPTION:
%% This function makes an array a vector. It is sometimes useful to
%% make the vector a column, row, or another dimension.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function A = vec(A, K)
narginchk(1,2);
if nargin<2
K = 1;
end
if K<1
error('vec::Use a positive integer for K.');
end
A = A(:);
A = shiftdim(A, 1-K);
end