MATLAB: How to access a vector of a 3D array

3d arrayaccess a vectorcell

I have a code :
%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1 : 3
for j = 1 : 5
P{i,j} = rand(1,2);
end
end
P{1,:}(2)
% I want to use this to access a certain vector from this 3D matrix P
% But matlab told me "Bad cell reference operation". Can anyone tell me
% the reason and how to do it? Thanks a lot!

Best Answer

Using a cell array is an inefficient representation. Better use a 3D array:
P = zeros(3, 5, 2);
for i = 1 : 3
for j = 1 : 5
P(i,j,:) = rand(1,2);
end
end
Now the indexing is trivial:
P(1, :, 2)