Matrix into Sub matrix of length 8
1 view (last 30 days)
Show older comments
Suppose I have a matrix like this A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24] (Length of a Matrix is always multiple of 8). I want to break it into sub matrices of length 8. I use reshape function but it does not work. Needs your help.
1 Comment
dpb
on 5 May 2019
What does "does not work" mean, precisely? Show your work, error received...or what you expected instead.
In general breaking into submatrices by creating new variables isn't productive manner in which to use, Matlab; instead reference subsets of an array as needed for which reshape can be quite useful...or, depending on what it is that is actually being done, blockproc in the image processing toolbox can be the ticket or various other specialty routines can be the answer--each depending on just what the actual end need may be...
Accepted Answer
Image Analyst
on 5 May 2019
What did you try for reshape? Did you try reshape(A, [], 8)?
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
a8 = reshape(A, [], 8)
% For example, to get 2nd subarray of 8 specify group as 1st index and : as second index.
out = a8(2,:)
More Answers (1)
KALYAN ACHARJYA
on 5 May 2019
Edited: madhan ravi
on 5 May 2019
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24];
n=length(A)/8;
C=num2cell(reshape(A,length(A)/n,n),1);
Access the subarrays in C{1}, C(2)..so on..
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!