Related to matrix partitioning?
2 views (last 30 days)
Show older comments
hi, I have a matrix of dimension (1*n) where n is a multiple of 4. I have to recursively partition it into four equal parts until n=4. How can I do it in matlab? Also I want all the partitioned matrices generated.
0 Comments
Answers (2)
David Sanchez
on 4 Mar 2014
Try this:
M=rand(1,32);
n=length(M);
A=zeros(4,n/4);
for k=1:4%k=k+1
A(k,:) = M((1+(k-1)*n/4):n*k/4);
end
Access your new matrices with A(k,:),for example:
>> A
A =
0.2599 0.8001 0.4314 0.9106 0.1818 0.2638 0.1455 0.1361
0.8693 0.5797 0.5499 0.1450 0.8530 0.6221 0.3510 0.5132
0.4018 0.0760 0.2399 0.1233 0.1839 0.2400 0.4173 0.0497
0.9027 0.9448 0.4909 0.4893 0.3377 0.9001 0.3692 0.1112
>> A(1,:)
ans =
0.2599 0.8001 0.4314 0.9106 0.1818 0.2638 0.1455 0.1361
0 Comments
Image Analyst
on 4 Mar 2014
Why recursively? Why mess with that complication? Unless you did some tricky stuff the 2,nd, 3rd, and 4th parts won't be the same height. Because you could extract the top 1/4 and then call the function on the remainder - the remaining 3/4. But 1/4 of 4/3 is 3/16, not one quarter, so the second time through it's shorter. Is that what you want? You didn't really say how n was getting changes at each level of recursion. Let's say it starts out at 32. How does n eventually get to 4? Do you divide by 4? How are the parts going to be equal each time? Let's say n=32 and you get 4 arrays of 8 rows each and then n now equals 32/4 = 8. Do you then recurse again and make 4 additional arrays of 2 rows each???
Why not simply get your 4 arrays like this:
m1 = m(1:rows/4,:);
m2 = m(rows/4+1:2*rows/4,:);
m3 = m(rows/2+1:3*rows/4,:);
m4 = m(3*rows/4+1:end,:);
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!