Thanks for the replies!
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to make a m*2 matrix into n number of 2x2 matrices
9 views (last 30 days)
Show older comments
Hey,
I have, A = 208x2 matrix. I wish to to spit this matrix into 104 2x2 matrices. I have tried using num2cell and mat2cell but have had no luck. Any help would be appreicated.
Thanks.
1 Comment
Accepted Answer
Stephen23
on 7 Sep 2022
A = rand(208,2);
C = mat2cell(A,2*ones(104,1),2)
C = 104×1 cell array
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
6 Comments
Dyl
on 7 Sep 2022
Thank you this works. I also have another question.
I wanted to create these 2x2 matrices so I could complete x=A\B to find the unknowns, the two forces acting upon a point. The 2x2 matrices is the matrices requried for each possible coordinate of the point subjected to two forces.
I have B = [0;62.4051] and when I try x=A\B it states there is an error 'Arguments must be numeric, char, or logical,' Do you know how I overcome this error?
Dyl
on 7 Sep 2022
I will elaborate on this to ensure clarity.
There are two unknown forces F1 and F2. They are acting upon point J. Point J is in a carteisan grid and therefore, has x number of possible locations. I have calculated the matrices for each location of point J and x=A\B is to calculate the two unknown forces (F1 and F2) for each possible J point. That is why there are 104 2x2 matrices because I wish to solve the system of equations for each point. However, Matlab will not allow for the calculations because of the error stated above.
Stephen23
on 7 Sep 2022
"I wanted to create these 2x2 matrices so I could complete x=A\B to find the unknowns..."
A = rand(208,2);
B = [0;62.4051];
1) loop over the cells, or use CELLFUN:
C = mat2cell(A,2*ones(104,1),2);
D = cellfun(@(a)a\B,C,'uni',0)
D = 104×1 cell array
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
{2×1 double}
D{1:5}
ans = 2×1
254.7375
-448.7160
ans = 2×1
-201.0346
269.0615
ans = 2×1
-15.4333
244.1028
ans = 2×1
-37.7452
115.8506
ans = 2×1
242.8873
-256.4729
2) avoid the cell array entirely: call PERMUTE&RESHAPE the data, and then call PAGEMLDIVIDE: https://www.mathworks.com/help/matlab/ref/pagemldivide.html
N = permute(reshape(A.',2,2,[]),[2,1,3]);
Z = pagemldivide(N,B)
Z =
Z(:,:,1) =
254.7375
-448.7160
Z(:,:,2) =
-201.0346
269.0615
Z(:,:,3) =
-15.4333
244.1028
Z(:,:,4) =
-37.7452
115.8506
Z(:,:,5) =
242.8873
-256.4729
Z(:,:,6) =
399.4539
-465.6991
Z(:,:,7) =
103.4845
-57.6442
Z(:,:,8) =
201.3918
-300.9553
Z(:,:,9) =
71.7649
-11.7430
Z(:,:,10) =
-140.2163
121.2142
Z(:,:,11) =
-249.7239
339.4336
Z(:,:,12) =
-74.7253
92.1409
Z(:,:,13) =
-71.2602
76.4186
Z(:,:,14) =
-51.0881
208.3873
Z(:,:,15) =
137.9599
-54.1809
Z(:,:,16) =
87.1851
-38.8293
Z(:,:,17) =
213.9198
-256.6160
Z(:,:,18) =
-184.3306
387.9273
Z(:,:,19) =
-83.0493
184.3273
Z(:,:,20) =
190.0474
-180.4605
Z(:,:,21) =
93.1972
-162.9662
Z(:,:,22) =
1.0e+03 *
-1.6875
1.1888
Z(:,:,23) =
-157.0278
165.5029
Z(:,:,24) =
1.0e+03 *
-1.1328
0.6363
Z(:,:,25) =
1.0e+03 *
2.7605
-1.5682
Z(:,:,26) =
-141.5120
226.2982
Z(:,:,27) =
457.9589
-677.7478
Z(:,:,28) =
-43.5975
101.2070
Z(:,:,29) =
294.5180
-179.8068
Z(:,:,30) =
455.4697
-141.1948
Z(:,:,31) =
174.2676
-91.3634
Z(:,:,32) =
291.5284
-317.2197
Z(:,:,33) =
1.0e+03 *
2.6183
-1.0546
Z(:,:,34) =
1.0e+03 *
-3.6468
3.4186
Z(:,:,35) =
-26.2973
82.2843
Z(:,:,36) =
92.9864
-399.6707
Z(:,:,37) =
-313.3782
334.0579
Z(:,:,38) =
1.0e+03 *
0.5017
-3.2983
Z(:,:,39) =
-25.1379
78.3355
Z(:,:,40) =
-60.8443
158.5588
Z(:,:,41) =
-126.9928
245.6587
Z(:,:,42) =
138.7083
-77.8317
Z(:,:,43) =
-73.1815
99.4966
Z(:,:,44) =
-29.2873
152.2512
Z(:,:,45) =
117.8240
-1.3511
Z(:,:,46) =
377.8151
-423.7863
Z(:,:,47) =
-972.7903
398.6766
Z(:,:,48) =
-718.3835
784.9308
Z(:,:,49) =
-347.1907
359.5996
Z(:,:,50) =
1.0e+03 *
3.4663
-1.6957
Z(:,:,51) =
-679.9005
433.6712
Z(:,:,52) =
85.1131
-13.1754
Z(:,:,53) =
74.8836
-2.8372
Z(:,:,54) =
139.1516
-24.7928
Z(:,:,55) =
-20.8858
235.3895
Z(:,:,56) =
-37.8064
109.0375
Z(:,:,57) =
-80.3218
92.2070
Z(:,:,58) =
1.0e+03 *
1.1045
-1.3925
Z(:,:,59) =
178.5230
-47.1629
Z(:,:,60) =
407.5570
-464.2490
Z(:,:,61) =
1.0e+03 *
1.7465
-0.3442
Z(:,:,62) =
-16.3257
455.7497
Z(:,:,63) =
-23.1154
132.7811
Z(:,:,64) =
1.0e+04 *
-1.6290
0.9430
Z(:,:,65) =
-27.6003
81.3097
Z(:,:,66) =
1.0e+03 *
-1.4705
1.7093
Z(:,:,67) =
1.0e+03 *
-1.9158
1.4897
Z(:,:,68) =
690.1267
-526.2135
Z(:,:,69) =
86.3370
-27.2462
Z(:,:,70) =
-50.0804
285.7423
Z(:,:,71) =
-25.8221
79.6419
Z(:,:,72) =
81.4405
-87.0205
Z(:,:,73) =
-12.2335
74.8112
Z(:,:,74) =
1.0e+03 *
-0.5944
1.2712
Z(:,:,75) =
112.3409
-86.8035
Z(:,:,76) =
98.5285
-13.9627
Z(:,:,77) =
-149.9494
175.6624
Z(:,:,78) =
167.1853
-77.1778
Z(:,:,79) =
174.2291
-206.0751
Z(:,:,80) =
113.1018
-19.5503
Z(:,:,81) =
215.3669
-47.3075
Z(:,:,82) =
-162.0178
200.6032
Z(:,:,83) =
885.2354
-250.7677
Z(:,:,84) =
256.4040
-164.9046
Z(:,:,85) =
1.0e+03 *
-0.0817
1.3533
Z(:,:,86) =
-23.8031
74.5642
Z(:,:,87) =
-124.8749
114.4583
Z(:,:,88) =
-56.6865
151.5106
Z(:,:,89) =
-20.6243
84.8720
Z(:,:,90) =
81.2266
-30.3164
Z(:,:,91) =
82.7109
-16.0798
Z(:,:,92) =
255.3523
-206.2097
Z(:,:,93) =
-423.2773
196.9388
Z(:,:,94) =
1.0e+03 *
-0.4201
1.9390
Z(:,:,95) =
321.7179
-89.7420
Z(:,:,96) =
-306.0857
229.0526
Z(:,:,97) =
-31.1052
97.7231
Z(:,:,98) =
128.2371
-167.8192
Z(:,:,99) =
107.3783
-63.8622
Z(:,:,100) =
1.0e+03 *
-1.1572
0.8569
Z(:,:,101) =
262.9194
-369.1406
Z(:,:,102) =
-59.1690
144.2688
Z(:,:,103) =
226.9019
-171.2881
Z(:,:,104) =
73.9992
-38.2548
Dyl
on 8 Sep 2022
Hey I have another question I reshaped the 2x1x104 matrix into a 13x8 matrix where I only took one value for each val(:,:,1) , valu(:,:,2) etc..
13x8 = [-28,-64 ... ;-33,-74...]
I wanted the values to insert into the 13x8 matric column wise so 1-8 for row 1 then 1-8 for row 2 so on, but they inserted row wise. How can I make the matrix I am after? Thanks in advance.
More Answers (2)
KSSV
on 7 Sep 2022
A = rand(208,2) ;
[r,c] = size(A);
nlay = 104 ;
out = permute(reshape(A',[c,r/nlay,nlay]),[2,1,3]);
Abderrahim. B
on 7 Sep 2022
Split A
A = randi(10, 208, 2) ; % a mtarix of size 208x2
size(A)
ans = 1×2
208 2
B = reshape(A, 2, 2, []) ;
Access 2x2 matrices
B1 = B(:,:,1)
B1 = 2×2
5 7
9 6
B2 = B(:,:,2)
B2 = 2×2
8 8
4 1
Hope this helps
2 Comments
Stephen23
on 7 Sep 2022
Edited: Stephen23
on 7 Sep 2022
Note that this method does not keep the 2x2 matrices of the original matrix:
A = randi(10, 208, 2)
A = 208×2
3 1
9 7
9 3
4 9
8 7
5 5
4 4
9 1
8 10
10 8
B = reshape(A, 2,2,[]) % not the same matrices
B =
B(:,:,1) =
3 9
9 4
B(:,:,2) =
8 4
5 9
B(:,:,3) =
8 5
10 4
B(:,:,4) =
2 1
10 10
B(:,:,5) =
2 5
7 9
B(:,:,6) =
4 7
4 9
B(:,:,7) =
6 9
4 6
B(:,:,8) =
10 9
2 3
B(:,:,9) =
10 4
6 4
B(:,:,10) =
8 5
2 5
B(:,:,11) =
4 2
6 4
B(:,:,12) =
5 1
6 8
B(:,:,13) =
7 4
4 6
B(:,:,14) =
3 7
6 6
B(:,:,15) =
4 4
7 9
B(:,:,16) =
9 6
2 10
B(:,:,17) =
8 9
7 6
B(:,:,18) =
3 3
2 4
B(:,:,19) =
6 1
6 3
B(:,:,20) =
4 3
10 10
B(:,:,21) =
5 9
6 5
B(:,:,22) =
2 2
8 10
B(:,:,23) =
4 8
3 7
B(:,:,24) =
9 7
2 3
B(:,:,25) =
8 10
6 9
B(:,:,26) =
6 7
8 4
B(:,:,27) =
7 10
10 1
B(:,:,28) =
3 9
7 4
B(:,:,29) =
4 3
9 10
B(:,:,30) =
4 5
3 2
B(:,:,31) =
2 5
9 5
B(:,:,32) =
2 9
9 4
B(:,:,33) =
2 1
6 9
B(:,:,34) =
8 1
3 4
B(:,:,35) =
2 4
4 3
B(:,:,36) =
7 1
5 4
B(:,:,37) =
10 7
1 10
B(:,:,38) =
8 1
1 10
B(:,:,39) =
10 4
1 8
B(:,:,40) =
2 7
9 7
B(:,:,41) =
3 10
2 7
B(:,:,42) =
8 5
9 5
B(:,:,43) =
3 10
10 1
B(:,:,44) =
7 9
2 3
B(:,:,45) =
2 10
9 10
B(:,:,46) =
8 1
9 6
B(:,:,47) =
5 8
7 9
B(:,:,48) =
2 2
3 2
B(:,:,49) =
3 2
3 5
B(:,:,50) =
4 2
9 3
B(:,:,51) =
7 10
10 3
B(:,:,52) =
9 5
3 3
B(:,:,53) =
1 3
7 9
B(:,:,54) =
7 4
5 1
B(:,:,55) =
10 2
8 10
B(:,:,56) =
7 8
8 4
B(:,:,57) =
3 3
6 1
B(:,:,58) =
9 2
5 6
B(:,:,59) =
10 9
6 3
B(:,:,60) =
1 9
5 4
B(:,:,61) =
5 9
4 3
B(:,:,62) =
7 9
8 5
B(:,:,63) =
3 7
3 10
B(:,:,64) =
8 10
10 8
B(:,:,65) =
6 10
9 4
B(:,:,66) =
8 2
7 3
B(:,:,67) =
3 6
5 3
B(:,:,68) =
7 5
5 6
B(:,:,69) =
3 6
6 10
B(:,:,70) =
4 4
6 1
B(:,:,71) =
2 7
6 8
B(:,:,72) =
1 6
1 5
B(:,:,73) =
7 1
10 1
B(:,:,74) =
1 3
4 6
B(:,:,75) =
1 4
5 9
B(:,:,76) =
10 10
9 1
B(:,:,77) =
10 2
4 4
B(:,:,78) =
4 8
8 3
B(:,:,79) =
10 1
10 8
B(:,:,80) =
6 5
7 9
B(:,:,81) =
10 9
8 5
B(:,:,82) =
5 5
5 8
B(:,:,83) =
1 8
5 1
B(:,:,84) =
10 4
4 8
B(:,:,85) =
8 7
6 4
B(:,:,86) =
4 10
4 2
B(:,:,87) =
9 6
3 2
B(:,:,88) =
9 7
9 7
B(:,:,89) =
10 8
10 7
B(:,:,90) =
1 3
9 4
B(:,:,91) =
7 4
1 8
B(:,:,92) =
10 9
5 9
B(:,:,93) =
2 4
6 8
B(:,:,94) =
6 3
7 10
B(:,:,95) =
10 9
10 2
B(:,:,96) =
10 5
3 2
B(:,:,97) =
10 4
9 4
B(:,:,98) =
10 5
9 8
B(:,:,99) =
5 7
6 7
B(:,:,100) =
7 10
1 9
B(:,:,101) =
1 2
7 9
B(:,:,102) =
8 3
5 6
B(:,:,103) =
10 10
4 8
B(:,:,104) =
6 10
3 4
To keep the original matrices requires taing into account the order of elements stored in memory:
B = permute(reshape(A.',2,2,[]),[2,1,3])
B =
B(:,:,1) =
3 1
9 7
B(:,:,2) =
9 3
4 9
B(:,:,3) =
8 7
5 5
B(:,:,4) =
4 4
9 1
B(:,:,5) =
8 10
10 8
B(:,:,6) =
5 2
4 10
B(:,:,7) =
2 7
10 8
B(:,:,8) =
1 8
10 4
B(:,:,9) =
2 3
7 6
B(:,:,10) =
5 3
9 1
B(:,:,11) =
4 9
4 5
B(:,:,12) =
7 2
9 6
B(:,:,13) =
6 10
4 6
B(:,:,14) =
9 9
6 3
B(:,:,15) =
10 1
2 5
B(:,:,16) =
9 9
3 4
B(:,:,17) =
10 5
6 4
B(:,:,18) =
4 9
4 3
B(:,:,19) =
8 7
2 8
B(:,:,20) =
5 9
5 5
B(:,:,21) =
4 3
6 3
B(:,:,22) =
2 7
4 10
B(:,:,23) =
5 8
6 10
B(:,:,24) =
1 10
8 8
B(:,:,25) =
7 6
4 9
B(:,:,26) =
4 10
6 4
B(:,:,27) =
3 8
6 7
B(:,:,28) =
7 2
6 3
B(:,:,29) =
4 3
7 5
B(:,:,30) =
4 6
9 3
B(:,:,31) =
9 7
2 5
B(:,:,32) =
6 5
10 6
B(:,:,33) =
8 3
7 6
B(:,:,34) =
9 6
6 10
B(:,:,35) =
3 4
2 6
B(:,:,36) =
3 4
4 1
B(:,:,37) =
6 2
6 6
B(:,:,38) =
1 7
3 8
B(:,:,39) =
4 1
10 1
B(:,:,40) =
3 6
10 5
B(:,:,41) =
5 7
6 10
B(:,:,42) =
9 1
5 1
B(:,:,43) =
2 1
8 4
B(:,:,44) =
2 3
10 6
B(:,:,45) =
4 1
3 5
B(:,:,46) =
8 4
7 9
B(:,:,47) =
9 10
2 9
B(:,:,48) =
7 10
3 1
B(:,:,49) =
8 10
6 4
B(:,:,50) =
10 2
9 4
B(:,:,51) =
6 4
8 8
B(:,:,52) =
7 8
4 3
B(:,:,53) =
7 10
10 10
B(:,:,54) =
10 1
1 8
B(:,:,55) =
3 6
7 7
B(:,:,56) =
9 5
4 9
B(:,:,57) =
4 10
9 8
B(:,:,58) =
3 9
10 5
B(:,:,59) =
4 5
3 5
B(:,:,60) =
5 5
2 8
B(:,:,61) =
2 1
9 5
B(:,:,62) =
5 8
5 1
B(:,:,63) =
2 10
9 4
B(:,:,64) =
9 4
4 8
B(:,:,65) =
2 8
6 6
B(:,:,66) =
1 7
9 4
B(:,:,67) =
8 4
3 4
B(:,:,68) =
1 10
4 2
B(:,:,69) =
2 9
4 3
B(:,:,70) =
4 6
3 2
B(:,:,71) =
7 9
5 9
B(:,:,72) =
1 7
4 7
B(:,:,73) =
10 10
1 10
B(:,:,74) =
7 8
10 7
B(:,:,75) =
8 1
1 9
B(:,:,76) =
1 3
10 4
B(:,:,77) =
10 7
1 1
B(:,:,78) =
4 4
8 8
B(:,:,79) =
2 10
9 5
B(:,:,80) =
7 9
7 9
B(:,:,81) =
3 2
2 6
B(:,:,82) =
10 4
7 8
B(:,:,83) =
8 6
9 7
B(:,:,84) =
5 3
5 10
B(:,:,85) =
3 10
10 10
B(:,:,86) =
10 9
1 2
B(:,:,87) =
7 10
2 3
B(:,:,88) =
9 5
3 2
B(:,:,89) =
2 10
9 9
B(:,:,90) =
10 4
10 4
B(:,:,91) =
8 10
9 9
B(:,:,92) =
1 5
6 8
B(:,:,93) =
5 5
7 6
B(:,:,94) =
8 7
9 7
B(:,:,95) =
2 7
3 1
B(:,:,96) =
2 10
2 9
B(:,:,97) =
3 1
3 7
B(:,:,98) =
2 2
5 9
B(:,:,99) =
4 8
9 5
B(:,:,100) =
2 3
3 6
B(:,:,101) =
7 10
10 4
B(:,:,102) =
10 10
3 8
B(:,:,103) =
9 6
3 3
B(:,:,104) =
5 10
3 4
Abderrahim. B
on 7 Sep 2022
Thanks @Stephen23. But he does not mention that the order must be te same as in the original matrix!
See Also
Categories
Find more on Creating and Concatenating 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)