I'm stuck with a basic problem in my MPEG-1 compression. I have to produce macroblocks within a image. A macroblock consists of 16 x 16 pixels - where 4 x 8x8 is luminance, 1 x 8x8 is Cb and 1 x 8x8 Cr. In MATLAB I want to produce a cell matrix containing this. Any suggestions?
It sounds like you're trying to figure out the best way to collect your various 8-by-8 matrices into a single variable. There are a few ways you can do this (with the first option being the one I would choose for your case):
You can use the CAT function to stack matrices of the same size along a given dimension. For example, if you have your 4 8-by-8 luminance matrices in the variables L1
, L2
, L3
, and L4
, the following will concatenate them into an 8-by-8-by-4 matrix:
luminance = cat(3,L1,L2,L3,L4);
You could also add the additional Cb
and Cr
matrices to create an 8-by-8-by-6 matrix:
macroBlock = cat(3,L1,L2,L3,L4,Cb,Cr);
%# OR...
macroBlock = cat(3,luminance,Cb,Cr); %# Using luminance variable from above
You can then index macroBlock
in the following way to access whatever 8-by-8 matrices you need:
L2 = macroBlock(:,:,2); %# Get the second luminance matrix
Cb = macroBlock(:,:,5); %# Get the Cb matrix
Since all of your matrices are the same size, the above concatenation option is probably best. However, another option (which is especially useful if you want to store data of varying size, type, or dimension) is to use cell arrays. The following creates a 1-by-6 cell array containing the above matrices:
macroBlock = {L1 L2 L3 L4 Cb Cr};
You can then index macroBlock
in the following way to access whatever 8-by-8 matrices you need:
L2 = macroBlock{2}; %# Get the second luminance matrix
Cb = macroBlock{5}; %# Get the Cb matrix
Another option is to use a structure to store your 8-by-8 matrices. A structure has the benefit that you can access data by field name as opposed to having to remember an index value. Here's how you would initialize a structure:
macroBlock = struct('L1',L1,'L2',L2,'L3',L3,'L4',L4,'Cb',Cb,'Cr',Cr);
%# OR...
macroBlock = struct();
macroBlock.L1 = L1;
macroBlock.L2 = L2;
macroBlock.L3 = L3;
macroBlock.L4 = L4;
macroBlock.Cb = Cb;
macroBlock.Cr = Cr;
The two syntaxes above create a structure with field names 'L1'
, 'L2'
, 'L3'
, 'L4'
, 'Cb'
, and 'Cr'
. You can then index macroBlock
in the following way to access whatever 8-by-8 matrices you need:
L2 = macroBlock.L2; %# Get the second luminance matrix
Cb = macroBlock.Cb; %# Get the Cb matrix