# How can I merge the matrices that produce different dimensions in the loop

2 vues (au cours des 30 derniers jours)
peter huang le 11 Déc 2022
Suppose my loop will generate matrices of different dimensions such as the first five matrices
first lap
28.984 30 58.984
28.984 13.943 42.927
28.984 30.082 59.066
28.984 14.959 43.943
28.984 44.025 73.009
2
30 13.943 43.943
30 0.041 30.041
30 27.968 57.968
30 29.066 59.066
30 27.424 57.424
30 57.968 87.968
3
28.44 29.528 57.968
28.44 30.544 58.984
28.44 57.968 86.408
4
15.041 57.968 73.009
15.041 44.025 59.066
15.041 73.009 88.05
5
13.943 30.082 44.025
13.943 0.082 14.025
I want to combine the variables generated in each loop
ex.
28.984 30 58.984
28.984 13.943 42.927
28.984 30.082 59.066
28.984 14.959 43.943
28.984 44.025 73.009
30 13.943 43.943
30 0.041 30.041
30 27.968 57.968
30 29.066 59.066
30 27.424 57.424
30 57.968 87.968
28.44 29.528 57.968
28.44 30.544 58.984
28.44 57.968 86.408
15.041 57.968 73.009
15.041 44.025 59.066
15.041 73.009 88.05
13.943 30.082 44.025
13.943 0.082 14.025
I tried writing directly into a new variable, but each time the length is different and it is overwritten
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (3)

Stephen23 le 11 Déc 2022
Modifié(e) : Stephen23 le 11 Déc 2022
Simply put them all into one cell array and then concatenate after the loop:
N = the number of loop iterations
C = cell(1,N);
for k = 1:N
C{k} = the matrix you want to store
end
M = vertcat(C{:})
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

KALYAN ACHARJYA le 11 Déc 2022
Modifié(e) : KALYAN ACHARJYA le 11 Déc 2022
#Example
data_store=cell(5,1)
data_store = 5×1 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
for i=1:5
data=rand(i,3)
data_store{i}=data;
end
data = 1×3
0.0183 0.2498 0.3910
data = 2×3
0.4556 0.6126 0.5924 0.6746 0.9996 0.2717
data = 3×3
0.9221 0.4498 0.5734 0.3334 0.2563 0.7197 0.8898 0.4627 0.5413
data = 4×3
0.0941 0.3222 0.2912 0.0120 0.0743 0.3047 0.3163 0.2125 0.2685 0.5703 0.5373 0.7103
data = 5×3
0.8565 0.5857 0.5728 0.5970 0.9831 0.4692 0.1349 0.3714 0.8664 0.6788 0.2480 0.0239 0.8564 0.3289 0.9417
dat=cell2mat(data_store)
dat = 15×3
0.0183 0.2498 0.3910 0.4556 0.6126 0.5924 0.6746 0.9996 0.2717 0.9221 0.4498 0.5734 0.3334 0.2563 0.7197 0.8898 0.4627 0.5413 0.0941 0.3222 0.2912 0.0120 0.0743 0.3047 0.3163 0.2125 0.2685 0.5703 0.5373 0.7103
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

VBBV le 11 Déc 2022
Use [ ] inside the loop
for k = 1:10
A = rand(5,3);
B = rand(6,3);
C = rand(3,3);
D = rand(2,3);
Merge(:,:,k) = [A;B;C;D];
end
Merge % combined values as matrix
Merge =
Merge(:,:,1) = 0.4013 0.1239 0.6253 0.0575 0.6757 0.9368 0.6054 0.1549 0.5772 0.1325 0.2943 0.9639 0.1470 0.6612 0.0220 0.2870 0.5827 0.2388 0.7312 0.4247 0.5491 0.5576 0.6883 0.7665 0.2159 0.9142 0.3130 0.1697 0.7011 0.3541 0.8969 0.3004 0.5008 0.6640 0.8416 0.7442 0.1350 0.2872 0.5360 0.8817 0.8070 0.2283 0.8939 0.3989 0.2912 0.1857 0.0025 0.3082 Merge(:,:,2) = 0.9083 0.3718 0.7547 0.7567 0.0680 0.0013 0.2502 0.0963 0.1959 0.1373 0.5104 0.3175 0.3609 0.6469 0.8319 0.2694 0.2631 0.4599 0.9861 0.4356 0.9003 0.1011 0.0077 0.4222 0.9156 0.7849 0.7488 0.0180 0.9633 0.0655 0.6900 0.3952 0.1234 0.8493 0.7677 0.1017 0.9807 0.2626 0.0635 0.7578 0.8257 0.4295 0.3333 0.1609 0.0389 0.0415 0.3197 0.1718 Merge(:,:,3) = 0.8455 0.8560 0.1672 0.8854 0.7139 0.6410 0.6673 0.9314 0.6411 0.1420 0.3113 0.8319 0.7799 0.9299 0.2699 0.3133 0.3431 0.5494 0.5225 0.2447 0.4290 0.4488 0.8724 0.9491 0.1973 0.6345 0.5107 0.8671 0.2723 0.9212 0.2808 0.8582 0.3671 0.0526 0.7805 0.2968 0.8329 0.1824 0.1772 0.6170 0.4640 0.0553 0.5558 0.3442 0.6181 0.4607 0.7660 0.4603 Merge(:,:,4) = 0.0731 0.3916 0.3055 0.2967 0.4049 0.9743 0.5666 0.4559 0.5670 0.8539 0.5190 0.2710 0.0706 0.7758 0.3927 0.7906 0.7653 0.6544 0.3889 0.4093 0.4368 0.1360 0.6452 0.3209 0.8387 0.7760 0.1953 0.1803 0.8682 0.0191 0.6442 0.4866 0.8624 0.6424 0.1472 0.9528 0.7384 0.0668 0.2558 0.1158 0.0448 0.6112 0.2941 0.8107 0.9121 0.8526 0.3854 0.0932 Merge(:,:,5) = 0.6052 0.8352 0.1316 0.1029 0.8254 0.3124 0.9234 0.6807 0.7693 0.0773 0.9834 0.8261 0.1536 0.6563 0.6946 0.4976 0.1211 0.4553 0.2499 0.4747 0.3692 0.6358 0.0990 0.6858 0.2322 0.7707 0.8059 0.7509 0.7066 0.9633 0.8539 0.2177 0.9448 0.2793 0.4442 0.8497 0.9969 0.9146 0.4039 0.2875 0.4864 0.5161 0.2265 0.4107 0.8316 0.9611 0.2594 0.1673 Merge(:,:,6) = 0.5266 0.1328 0.9968 0.8267 0.8494 0.5613 0.8899 0.5494 0.0108 0.9486 0.3950 0.5713 0.7804 0.5162 0.8452 0.6143 0.3971 0.0572 0.9394 0.8118 0.8778 0.2162 0.7978 0.2205 0.7541 0.7829 0.6563 0.4957 0.8281 0.5609 0.7650 0.2779 0.8776 0.2450 0.3081 0.4832 0.5828 0.6518 0.8811 0.4564 0.7928 0.8943 0.2251 0.3501 0.8329 0.9451 0.0632 0.0891 Merge(:,:,7) = 0.7692 0.4461 0.9644 0.4734 0.5234 0.8325 0.7735 0.8702 0.6563 0.2759 0.7548 0.4174 0.2914 0.2579 0.0562 0.1059 0.5451 0.8564 0.3397 0.7212 0.4660 0.1763 0.5526 0.2931 0.1584 0.9013 0.8095 0.6209 0.2377 0.7214 0.4278 0.2810 0.6115 0.0964 0.2654 0.8508 0.7475 0.0546 0.2377 0.0218 0.2716 0.9645 0.9191 0.0321 0.4831 0.1514 0.5137 0.6212 Merge(:,:,8) = 0.3294 0.6343 0.5948 0.3720 0.6073 0.0937 0.3811 0.9372 0.6604 0.4051 0.5465 0.4951 0.9453 0.9426 0.0910 0.7541 0.3798 0.9870 0.2794 0.1277 0.8975 0.3408 0.0500 0.0851 0.5950 0.9886 0.9369 0.8244 0.6924 0.2579 0.7462 0.5800 0.4700 0.4291 0.2433 0.4843 0.7770 0.8681 0.0873 0.9527 0.1664 0.1354 0.0801 0.6208 0.6704 0.5934 0.9052 0.5313 Merge(:,:,9) = 0.4093 0.9055 0.2112 0.1072 0.8252 0.5092 0.8709 0.6789 0.1874 0.4658 0.3886 0.9119 0.3406 0.5269 0.8839 0.0971 0.3159 0.9479 0.0395 0.7674 0.2077 0.4929 0.6431 0.8986 0.4726 0.1378 0.5373 0.8689 0.9096 0.1570 0.4559 0.5540 0.2322 0.7081 0.7302 0.6555 0.3976 0.7064 0.0746 0.8982 0.7447 0.0848 0.6105 0.9197 0.7606 0.7136 0.3730 0.2990 Merge(:,:,10) = 0.8527 0.3783 0.4198 0.6377 0.2912 0.9535 0.5892 0.3628 0.4902 0.8956 0.6733 0.0883 0.2066 0.9275 0.3247 0.7287 0.7245 0.7213 0.7262 0.7077 0.9712 0.2179 0.0094 0.0217 0.5646 0.9169 0.9540 0.5874 0.5577 0.7649 0.1011 0.2813 0.0348 0.9603 0.2688 0.0472 0.2156 0.4576 0.4638 0.3953 0.8757 0.3562 0.3292 0.2778 0.8646 0.0883 0.7493 0.7235
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by