How you would do it depends on how you want it to work.
For instance, if your 5-dimensional matrix contains two singleton (i.e., of size 1) dimensions that you want to remove, you could use squeeze():
X = randn(5,1,3,1,2);
Y = squeeze(X);
disp(X);
(:,:,1,1,1) =
-0.3821
-0.9261
0.0377
-0.3751
0.6377
(:,:,2,1,1) =
-0.8345
-0.1924
-0.7902
0.0368
-0.3870
(:,:,3,1,1) =
-0.8840
1.3846
-0.6376
0.6339
0.8134
(:,:,1,1,2) =
-0.9523
-0.5598
1.0810
0.7166
1.1387
(:,:,2,1,2) =
0.7773
0.3358
-0.5979
1.3993
-0.5095
(:,:,3,1,2) =
-0.4363
-0.3040
-0.1557
-1.0283
1.5534
disp(Y);
(:,:,1) =
-0.3821 -0.8345 -0.8840
-0.9261 -0.1924 1.3846
0.0377 -0.7902 -0.6376
-0.3751 0.0368 0.6339
0.6377 -0.3870 0.8134
(:,:,2) =
-0.9523 0.7773 -0.4363
-0.5598 0.3358 -0.3040
1.0810 -0.5979 -0.1557
0.7166 1.3993 -1.0283
1.1387 -0.5095 1.5534
Or you could use reshape() in that same situation:
Y = reshape(X,[5 3 2]);
disp(Y);
(:,:,1) =
-0.3821 -0.8345 -0.8840
-0.9261 -0.1924 1.3846
0.0377 -0.7902 -0.6376
-0.3751 0.0368 0.6339
0.6377 -0.3870 0.8134
(:,:,2) =
-0.9523 0.7773 -0.4363
-0.5598 0.3358 -0.3040
1.0810 -0.5979 -0.1557
0.7166 1.3993 -1.0283
1.1387 -0.5095 1.5534
Or even if there are no singleton dimensions, you could use reshape(), but you have to specify how you want this reshaping done:
X = randn(5,2,3,4,2);
disp(X);
(:,:,1,1,1) =
-1.5990 -1.4774
-0.2814 -1.0066
-0.1961 0.4082
-0.0850 -0.7829
-1.0714 -0.5700
(:,:,2,1,1) =
0.1118 -1.9138
2.2011 -0.4253
-2.3668 -0.9079
0.4306 0.7721
-0.5925 0.8626
(:,:,3,1,1) =
-0.6635 0.2661
-1.1877 1.9580
0.1238 -0.5403
-1.2074 -0.9911
-0.8459 -0.7236
(:,:,1,2,1) =
1.4873 1.1875
0.0843 0.5482
-1.5242 1.5693
-0.5216 1.2928
-0.8007 -0.4383
(:,:,2,2,1) =
1.2575 1.7517
-0.6058 0.0287
1.9479 0.1202
-0.6029 -0.0174
-0.5533 1.0998
(:,:,3,2,1) =
-0.3341 0.0497
0.2022 1.1645
1.1306 -1.0376
-0.6294 0.4023
2.2155 -0.8428
(:,:,1,3,1) =
-1.9050 -0.0878
-1.3216 -0.7829
-0.0930 0.3349
1.2693 -0.3298
0.4882 0.4263
(:,:,2,3,1) =
-0.4462 1.2260
0.0387 0.2094
-0.0832 1.4408
2.0849 -0.5376
0.3471 -1.1520
(:,:,3,3,1) =
1.2225 0.3516
3.5116 0.6621
0.8052 2.6316
-0.1157 1.3297
0.0241 0.4893
(:,:,1,4,1) =
1.3257 -1.1252
-1.2487 1.5519
0.4120 -0.8644
-0.7296 -1.5258
1.2875 0.1905
(:,:,2,4,1) =
0.3836 0.6030
0.8254 0.2551
-0.3455 1.1446
-0.6773 -1.3863
-1.5075 -0.5765
(:,:,3,4,1) =
0.3761 -0.9105
1.4721 -1.1455
0.4240 -0.1831
0.5730 -2.4002
-0.2211 1.8798
(:,:,1,1,2) =
-0.2634 -0.5790
-0.2404 -0.9705
1.4386 0.7587
-0.6913 -0.8247
-0.2514 -0.6161
(:,:,2,1,2) =
-0.0273 0.1439
1.6814 0.2454
0.8269 -1.0881
-1.1424 0.7971
-2.1742 0.8649
(:,:,3,1,2) =
0.0594 -2.0908
-0.1723 0.2557
-1.1110 -1.5074
0.1578 0.5286
-0.7027 1.9260
(:,:,1,2,2) =
-0.3547 0.1481
-0.9297 -1.0064
-0.1334 1.1872
-0.0308 -0.0943
-0.3632 -1.4703
(:,:,2,2,2) =
0.0866 0.0515
0.1770 -1.0495
-1.0910 1.2994
-0.1530 -0.1821
-0.1600 1.6377
(:,:,3,2,2) =
0.8820 -0.5114
-1.6949 0.1523
0.4375 2.1341
-0.3578 -1.5224
-0.4132 0.3853
(:,:,1,3,2) =
0.5756 0.7845
0.5070 -0.0190
-1.6021 -1.7109
0.8970 -2.1029
0.8054 2.3897
(:,:,2,3,2) =
-0.4548 -0.4863
-1.7089 -0.4617
1.2106 0.5118
0.1166 -0.3795
-0.8231 0.1280
(:,:,3,3,2) =
-0.3409 -1.1771
-1.0191 0.1713
0.8405 0.3731
-1.0565 -1.0445
-0.0011 -0.9263
(:,:,1,4,2) =
-0.0527 -1.1203
-1.0325 0.1015
-0.7649 1.2671
0.4507 -0.2430
0.6585 -0.1224
(:,:,2,4,2) =
0.2141 1.1805
-1.3787 0.3057
1.5172 0.2990
0.4007 -0.3995
-0.6900 1.3717
(:,:,3,4,2) =
1.1456 0.1479
-0.4306 -1.7615
0.8234 0.2880
0.3705 -0.0219
0.2025 -0.8276
Y = reshape(X,[10 12 2]);
disp(Y);
(:,:,1) =
-1.5990 0.1118 -0.6635 1.4873 1.2575 -0.3341 -1.9050 -0.4462 1.2225 1.3257 0.3836 0.3761
-0.2814 2.2011 -1.1877 0.0843 -0.6058 0.2022 -1.3216 0.0387 3.5116 -1.2487 0.8254 1.4721
-0.1961 -2.3668 0.1238 -1.5242 1.9479 1.1306 -0.0930 -0.0832 0.8052 0.4120 -0.3455 0.4240
-0.0850 0.4306 -1.2074 -0.5216 -0.6029 -0.6294 1.2693 2.0849 -0.1157 -0.7296 -0.6773 0.5730
-1.0714 -0.5925 -0.8459 -0.8007 -0.5533 2.2155 0.4882 0.3471 0.0241 1.2875 -1.5075 -0.2211
-1.4774 -1.9138 0.2661 1.1875 1.7517 0.0497 -0.0878 1.2260 0.3516 -1.1252 0.6030 -0.9105
-1.0066 -0.4253 1.9580 0.5482 0.0287 1.1645 -0.7829 0.2094 0.6621 1.5519 0.2551 -1.1455
0.4082 -0.9079 -0.5403 1.5693 0.1202 -1.0376 0.3349 1.4408 2.6316 -0.8644 1.1446 -0.1831
-0.7829 0.7721 -0.9911 1.2928 -0.0174 0.4023 -0.3298 -0.5376 1.3297 -1.5258 -1.3863 -2.4002
-0.5700 0.8626 -0.7236 -0.4383 1.0998 -0.8428 0.4263 -1.1520 0.4893 0.1905 -0.5765 1.8798
(:,:,2) =
-0.2634 -0.0273 0.0594 -0.3547 0.0866 0.8820 0.5756 -0.4548 -0.3409 -0.0527 0.2141 1.1456
-0.2404 1.6814 -0.1723 -0.9297 0.1770 -1.6949 0.5070 -1.7089 -1.0191 -1.0325 -1.3787 -0.4306
1.4386 0.8269 -1.1110 -0.1334 -1.0910 0.4375 -1.6021 1.2106 0.8405 -0.7649 1.5172 0.8234
-0.6913 -1.1424 0.1578 -0.0308 -0.1530 -0.3578 0.8970 0.1166 -1.0565 0.4507 0.4007 0.3705
-0.2514 -2.1742 -0.7027 -0.3632 -0.1600 -0.4132 0.8054 -0.8231 -0.0011 0.6585 -0.6900 0.2025
-0.5790 0.1439 -2.0908 0.1481 0.0515 -0.5114 0.7845 -0.4863 -1.1771 -1.1203 1.1805 0.1479
-0.9705 0.2454 0.2557 -1.0064 -1.0495 0.1523 -0.0190 -0.4617 0.1713 0.1015 0.3057 -1.7615
0.7587 -1.0881 -1.5074 1.1872 1.2994 2.1341 -1.7109 0.5118 0.3731 1.2671 0.2990 0.2880
-0.8247 0.7971 0.5286 -0.0943 -0.1821 -1.5224 -2.1029 -0.3795 -1.0445 -0.2430 -0.3995 -0.0219
-0.6161 0.8649 1.9260 -1.4703 1.6377 0.3853 2.3897 0.1280 -0.9263 -0.1224 1.3717 -0.8276
Y = reshape(X,[5 6 8]);
disp(Y);
(:,:,1) =
-1.5990 -1.4774 0.1118 -1.9138 -0.6635 0.2661
-0.2814 -1.0066 2.2011 -0.4253 -1.1877 1.9580
-0.1961 0.4082 -2.3668 -0.9079 0.1238 -0.5403
-0.0850 -0.7829 0.4306 0.7721 -1.2074 -0.9911
-1.0714 -0.5700 -0.5925 0.8626 -0.8459 -0.7236
(:,:,2) =
1.4873 1.1875 1.2575 1.7517 -0.3341 0.0497
0.0843 0.5482 -0.6058 0.0287 0.2022 1.1645
-1.5242 1.5693 1.9479 0.1202 1.1306 -1.0376
-0.5216 1.2928 -0.6029 -0.0174 -0.6294 0.4023
-0.8007 -0.4383 -0.5533 1.0998 2.2155 -0.8428
(:,:,3) =
-1.9050 -0.0878 -0.4462 1.2260 1.2225 0.3516
-1.3216 -0.7829 0.0387 0.2094 3.5116 0.6621
-0.0930 0.3349 -0.0832 1.4408 0.8052 2.6316
1.2693 -0.3298 2.0849 -0.5376 -0.1157 1.3297
0.4882 0.4263 0.3471 -1.1520 0.0241 0.4893
(:,:,4) =
1.3257 -1.1252 0.3836 0.6030 0.3761 -0.9105
-1.2487 1.5519 0.8254 0.2551 1.4721 -1.1455
0.4120 -0.8644 -0.3455 1.1446 0.4240 -0.1831
-0.7296 -1.5258 -0.6773 -1.3863 0.5730 -2.4002
1.2875 0.1905 -1.5075 -0.5765 -0.2211 1.8798
(:,:,5) =
-0.2634 -0.5790 -0.0273 0.1439 0.0594 -2.0908
-0.2404 -0.9705 1.6814 0.2454 -0.1723 0.2557
1.4386 0.7587 0.8269 -1.0881 -1.1110 -1.5074
-0.6913 -0.8247 -1.1424 0.7971 0.1578 0.5286
-0.2514 -0.6161 -2.1742 0.8649 -0.7027 1.9260
(:,:,6) =
-0.3547 0.1481 0.0866 0.0515 0.8820 -0.5114
-0.9297 -1.0064 0.1770 -1.0495 -1.6949 0.1523
-0.1334 1.1872 -1.0910 1.2994 0.4375 2.1341
-0.0308 -0.0943 -0.1530 -0.1821 -0.3578 -1.5224
-0.3632 -1.4703 -0.1600 1.6377 -0.4132 0.3853
(:,:,7) =
0.5756 0.7845 -0.4548 -0.4863 -0.3409 -1.1771
0.5070 -0.0190 -1.7089 -0.4617 -1.0191 0.1713
-1.6021 -1.7109 1.2106 0.5118 0.8405 0.3731
0.8970 -2.1029 0.1166 -0.3795 -1.0565 -1.0445
0.8054 2.3897 -0.8231 0.1280 -0.0011 -0.9263
(:,:,8) =
-0.0527 -1.1203 0.2141 1.1805 1.1456 0.1479
-1.0325 0.1015 -1.3787 0.3057 -0.4306 -1.7615
-0.7649 1.2671 1.5172 0.2990 0.8234 0.2880
0.4507 -0.2430 0.4007 -0.3995 0.3705 -0.0219
0.6585 -0.1224 -0.6900 1.3717 0.2025 -0.8276
Y = reshape(X,[5 2 24]);
disp(Y);
(:,:,1) =
-1.5990 -1.4774
-0.2814 -1.0066
-0.1961 0.4082
-0.0850 -0.7829
-1.0714 -0.5700
(:,:,2) =
0.1118 -1.9138
2.2011 -0.4253
-2.3668 -0.9079
0.4306 0.7721
-0.5925 0.8626
(:,:,3) =
-0.6635 0.2661
-1.1877 1.9580
0.1238 -0.5403
-1.2074 -0.9911
-0.8459 -0.7236
(:,:,4) =
1.4873 1.1875
0.0843 0.5482
-1.5242 1.5693
-0.5216 1.2928
-0.8007 -0.4383
(:,:,5) =
1.2575 1.7517
-0.6058 0.0287
1.9479 0.1202
-0.6029 -0.0174
-0.5533 1.0998
(:,:,6) =
-0.3341 0.0497
0.2022 1.1645
1.1306 -1.0376
-0.6294 0.4023
2.2155 -0.8428
(:,:,7) =
-1.9050 -0.0878
-1.3216 -0.7829
-0.0930 0.3349
1.2693 -0.3298
0.4882 0.4263
(:,:,8) =
-0.4462 1.2260
0.0387 0.2094
-0.0832 1.4408
2.0849 -0.5376
0.3471 -1.1520
(:,:,9) =
1.2225 0.3516
3.5116 0.6621
0.8052 2.6316
-0.1157 1.3297
0.0241 0.4893
(:,:,10) =
1.3257 -1.1252
-1.2487 1.5519
0.4120 -0.8644
-0.7296 -1.5258
1.2875 0.1905
(:,:,11) =
0.3836 0.6030
0.8254 0.2551
-0.3455 1.1446
-0.6773 -1.3863
-1.5075 -0.5765
(:,:,12) =
0.3761 -0.9105
1.4721 -1.1455
0.4240 -0.1831
0.5730 -2.4002
-0.2211 1.8798
(:,:,13) =
-0.2634 -0.5790
-0.2404 -0.9705
1.4386 0.7587
-0.6913 -0.8247
-0.2514 -0.6161
(:,:,14) =
-0.0273 0.1439
1.6814 0.2454
0.8269 -1.0881
-1.1424 0.7971
-2.1742 0.8649
(:,:,15) =
0.0594 -2.0908
-0.1723 0.2557
-1.1110 -1.5074
0.1578 0.5286
-0.7027 1.9260
(:,:,16) =
-0.3547 0.1481
-0.9297 -1.0064
-0.1334 1.1872
-0.0308 -0.0943
-0.3632 -1.4703
(:,:,17) =
0.0866 0.0515
0.1770 -1.0495
-1.0910 1.2994
-0.1530 -0.1821
-0.1600 1.6377
(:,:,18) =
0.8820 -0.5114
-1.6949 0.1523
0.4375 2.1341
-0.3578 -1.5224
-0.4132 0.3853
(:,:,19) =
0.5756 0.7845
0.5070 -0.0190
-1.6021 -1.7109
0.8970 -2.1029
0.8054 2.3897
(:,:,20) =
-0.4548 -0.4863
-1.7089 -0.4617
1.2106 0.5118
0.1166 -0.3795
-0.8231 0.1280
(:,:,21) =
-0.3409 -1.1771
-1.0191 0.1713
0.8405 0.3731
-1.0565 -1.0445
-0.0011 -0.9263
(:,:,22) =
-0.0527 -1.1203
-1.0325 0.1015
-0.7649 1.2671
0.4507 -0.2430
0.6585 -0.1224
(:,:,23) =
0.2141 1.1805
-1.3787 0.3057
1.5172 0.2990
0.4007 -0.3995
-0.6900 1.3717
(:,:,24) =
1.1456 0.1479
-0.4306 -1.7615
0.8234 0.2880
0.3705 -0.0219
0.2025 -0.8276
permute() and shiftdim() might also be useful.