Slice a large 2D matrix into a 3D one -- error

Hi, I'm trying to extract a series of 32x32 matrices of a large original 6601x32 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.

 采纳的回答

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

1 个评论

Thanks a lot! I've should have explained my problem a little better.. I did not say that had to remove one row from each frame which had some metadata like the time..etc. But thanks to your comment I realized that when I imported the data matrix it was adding an end row with NAN in the fields, so I removed it.
Then the 2D row size was 6600 and not 6601, and removing the metadata rows I was able to create the 3D matrix with all the slices.
Cheers

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by