I have a cell array that is three dimensional i.e. 16x10x3 and I would like for all values in :,:,1
:,:,2
and :,:,3
to be extracted into a column of a matrix each.
I thought about pre-allocating a matrix and then just running a basic loop such as:
for m=1:3
mat(:,m) = ([a{:,:,m}]);
end
Is there a more efficient way of doing this without having to rely on a loop?
EDIT: There are different numbers of values between :,:,1/2/3.
It's high time you get into bsxfun
! Here's the implementation -
%// Get the number of elements in each column of the input cell array
lens = sum(cellfun('length',reshape(a,[],size(a,3))),1)
%// Store the maximum number of elements possible in any column of output array
max_lens = max(lens)
%// Setup output array, with no. of rows as max number of elements in each column
%// and no. of columns would be same as the no. of columns in input cell array
mat = zeros(max_lens,numel(lens))
%// Create as mask that has ones to the "extent" of number of elements in
%// each column of the input cell array using the lengths
mask = bsxfun(@le,[1:max_lens]',lens) %//'
%// Finally, store the values from input cell array into masked positions
mat(mask) = [a{:}]