To vectorize a matrix in MATLAB, you can execute this simple command:
A = reshape(1:9, 3, 3)
% A =
% [1 4 7]
% [2 5 8]
% [3 6 9]
b = A(:)
% b = [1 2 3 4 5 6 7 8 9]'
But how about if you have a matrix that you want to first slice, then vectorize? How do you go about doing this without assigning to a temporary variable?
Let's say A is now:
A = reshape(1:27, 3, 3, 3)
% A(:,:,1) =
% [1 4 7]
% [2 5 8]
% [3 6 9]
% A(:,:,2) =
% [10 13 16]
% [11 14 17]
% [12 15 18]
% A(:,:,3) =
% [19 22 25]
% [20 23 26]
% [21 24 27]
If you run
b = A(:,:,1)(:)
% Error: ()-indexing must appear last in an index expression.
Is there some function, vectorize(A) that gives this functionality?
b = vectorize(A(:,:,1))
% b = [1 2 3 4 5 6 7 8 9]'
Or if not a function, is there an alternative method than
tmp = A(:,:,1)
b = tmp(:)
Thanks in advance for the guidance!