MATLAB: Slice a large 2D matrix into a 3D one — error

2d3dindexindexingMATLABmatrix

Hi, I'm trying to extract a series of 32×32 matrices of a large original 6601×32 matrix, and save them into a 3D (32x32x200) one. Each matrix is a sample I've logged from a scanning device (200 in total). I'm having this error but I couldn't find out why it happens or how to fix it.
% Index exceeds matrix dimensions.
This is the part of my program that throws the error:
A = cell2mat(raw);
[m,n] = size(A);
% Create 200 Frames 32x32
framesize = 32 % Number of Rows and Columns of Each Frame
Frame3d = zeros(32,32,200); % Prealocate Frames
% % Now scan A, getting each frame and putting it as a slice of a 3D array.
framenumber=1;
for row = 1 : 33 : m
row1 = row + 1; % framesizer = 32, and row1 = row +1 to avoid first row
row2 = row1 + framesize - 1;
Frame3d(:,:,framenumber) = A(row1:row2,:); % Assign the submatrix to the slice --> Here throws the error
framenumber = framenumber + 1;
end
Basically I cannot figure it out.
Kind regards.

Best Answer

They are not the same size:
>> 6601*32
ans =
211232
>> 32*32*200
ans =
204800
Why do you think the reshaped matrix should contain all of the input data?
Try this:
% Make up sample data.
frame2d = randi(255, 6601,32, 'uint8');
% Preallocate output data.
frame3d = randi(255, 32,32,200, 'uint8');
% Transfer 32x32 chunks to output array.
slice = 1;
for row = 1 : 32 : size(frame2d, 1) - 31
frame3d(:, :, slice) = frame2d(row:row+31, :);
slice = slice + 1;
end