Azzera filtri
Azzera filtri

Save array values separetly in cells during a for loop

2 visualizzazioni (ultimi 30 giorni)
Hello,
I have a for loop that returns in each iteration a 6x1 array. I want to save the value of the 6 elements in a different cell so in the end I have a 6x1 cell with 2500 (for loop iteration) elements.
function [qdd,qds,qs]= qsiguiente(EM,torque,E2,zvel,deltaT,zpos,n,i)
%qdd is a 6x1 array
qdd= EM\(torque-E2);
%speed
for indx=1:6
%%%%%%
% MY APROACH (not working) convert this 6x1 array to a 6x1
% cell with 2500 iterations
qdd{indx,1}=qdd(indx,1);
%%%%%%
qdd{indx,1}=qdd(indx,1);
qds(indx,1)=zvel{indx}(1,i)+deltaT*qdd(indx,1);
% position
qs(indx,1)=zpos{indx}(1,i)+deltaT*qds(indx,1);
end
end

Risposta accettata

Voss
Voss il 28 Gen 2022
You cannot convert a numeric matrix to a cell array in-place like that, but you can make a new cell array variable and do it:
qdd = rand(6,1);
qdd_cell = cell(size(qdd));
for indx = 1:numel(qdd)
qdd_cell{indx,1} = qdd(indx,1);
end
disp(qdd); disp(qdd_cell);
0.1979 0.8719 0.6815 0.9625 0.4098 0.6827 {[0.1979]} {[0.8719]} {[0.6815]} {[0.9625]} {[0.4098]} {[0.6827]}
Or, simpler:
qdd_cell = num2cell(qdd);
disp(qdd); disp(qdd_cell);
0.1979 0.8719 0.6815 0.9625 0.4098 0.6827 {[0.1979]} {[0.8719]} {[0.6815]} {[0.9625]} {[0.4098]} {[0.6827]}
However, I'm not sure a cell array is the best choice for what you want to do. If the idea is that each iteration adds a new element to each cell of qdd_cell, why not just use a 2D numeric matrix?
That is, instead of doing this:
qdd_cell = num2cell(qdd);
N_iterations = 100;
for jj = 1:N_iterations
for indx = 1:numel(qdd)
qdd_cell{indx}(end+1) = randn();
end
end
disp(qdd_cell);
{[0.1979 1.3184 -0.4066 0.2984 0.4895 0.5974 -0.0229 -1.1849 -0.4800 -1.0499 -0.4783 1.2495 -0.3460 2.1856 -0.3872 0.4493 0.3742 -0.9162 2.1993 0.7583 -0.3564 0.3000 1.0370 -0.3882 1.1593 1.1833 -0.4546 0.5094 -0.0470 … ]} {[ 0.8719 -1.1574 2.6773 -0.4751 0.0783 0.0431 -0.0412 -1.0519 -0.3133 -1.6014 -2.7592 -0.4468 -0.4487 0.3383 1.3248 1.3718 -0.1225 -0.9849 -0.1420 -2.1463 0.1925 0.8244 0.7885 -0.2629 -0.2415 -0.2744 0.7501 0.6819 … ]} {[ 0.6815 1.3340 -0.1197 2.2514 0.0535 0.4672 -0.4069 -0.3475 -1.4043 0.9099 0.8901 1.2388 -1.7230 -1.0479 0.0235 -0.2098 0.7027 -0.2710 -1.8637 0.4115 -0.3248 -0.7923 -0.6348 -0.5695 0.2774 0.7003 0.4823 -0.3350 … ]} {[ 0.9625 0.8388 -0.4657 -3.1278 -0.5768 0.6173 1.5956 0.8062 -0.7343 -0.5167 -0.5327 0.9215 -1.1369 -2.6464 0.4665 -0.5237 -0.2613 -0.1988 -0.5226 -0.6931 1.2769 -1.4648 0.4755 0.7316 2.7159 0.0724 1.6769 -0.7795 … ]} {[ 0.4098 -0.7810 -0.5091 0.2972 0.5440 -0.1566 -1.0752 -1.2345 1.2092 -1.0769 -0.1973 2.1954 2.9317 0.1247 0.5735 -1.5319 -0.8353 -0.4097 -1.3167 1.7274 -0.3101 -0.4680 0.0488 0.5104 -0.5825 -0.3463 -1.4754 -1.3549 … ]} {[ 0.6827 -0.3138 -0.7544 -2.0646 0.8569 -0.4641 -0.0817 -0.2468 0.0918 0.2039 1.7584 0.4713 -0.0365 0.0734 1.0328 0.0663 -0.3027 -0.1675 -1.0524 -0.0111 0.7884 -1.6948 -1.2475 -1.2604 -1.8228 1.0890 0.3902 0.0135 … ]}
You can do this:
qdd_matrix = NaN(numel(qdd),N_iterations);
for jj = 1:N_iterations
for indx = 1:numel(qdd)
qdd_matrix(indx,jj) = randn();
end
end
disp(qdd_matrix);
Columns 1 through 22 -2.1150 -1.2348 0.9292 -1.0466 0.0433 0.6537 -0.5743 1.5840 0.5644 -1.0570 0.3420 -1.5404 -0.1146 1.2409 1.0188 1.1134 0.1178 -1.4640 -0.8415 -0.7508 0.1603 0.6251 0.2291 -0.4691 -0.6938 -0.9004 -0.1304 -1.6158 0.3462 0.0924 0.5912 -0.5214 -0.8440 -0.7224 1.4801 0.1292 -0.5733 0.1301 -0.0957 2.2561 1.1594 1.3208 -0.7286 1.6529 0.9144 0.0766 -1.4980 0.4476 1.0194 -1.0746 -0.1578 -1.9101 0.4913 -0.7782 0.4552 -1.4068 -0.3081 -1.5103 -0.4787 -0.2321 1.4489 -0.7285 0.0333 0.7718 0.6036 0.6451 0.1516 -0.8545 -1.7877 1.8617 1.2383 -0.5886 -0.6822 0.7587 0.9764 0.2486 -0.8648 -0.6014 0.1770 -0.8365 0.1741 1.3796 0.7802 -0.5137 0.7908 0.4076 0.2563 -0.6937 0.6228 0.7938 -0.0501 -1.6762 -0.1991 -0.4979 -2.0716 -0.3895 0.4143 0.0009 1.2718 -0.5522 -0.6777 0.1095 0.2123 1.1560 0.8160 -1.4190 -0.6498 -1.7572 -1.5819 -0.4839 -1.5969 1.6190 -1.1820 0.8032 0.1683 -0.5896 -0.8892 -1.8902 -2.8493 -1.7496 0.1985 -2.3295 0.9767 -1.4918 -0.3436 -0.4942 0.5727 0.7649 0.5417 -0.5040 -0.9051 1.9272 Columns 23 through 44 1.1653 -1.5050 0.1995 1.5419 0.9729 -0.1748 -0.1478 -0.2871 -0.5201 -1.9360 0.0029 -0.0855 0.6906 0.5626 -0.0357 0.1421 -0.7448 0.9392 0.3399 -0.8960 -1.0805 1.0545 1.2576 -0.3041 1.1289 -0.7453 0.9692 -0.2980 0.9635 0.8867 -0.3714 0.2878 0.8496 -0.9332 0.2838 -0.8079 0.0897 1.8997 0.9894 -0.6837 -0.5924 -0.3025 0.1638 0.8526 -1.0075 -1.3231 -1.4752 0.9485 -0.2493 0.3257 -0.2919 0.4093 0.0534 -0.0797 0.1167 2.0805 0.1490 -1.5117 -0.4243 -0.1472 0.0891 0.7442 0.1935 -1.0764 -0.0834 0.6888 0.2471 -0.5945 -1.5464 1.2730 0.5871 -1.8472 -0.0759 0.8716 0.2530 0.4055 -0.3894 1.2459 -0.5138 -0.1815 -0.7127 1.0157 -1.3347 0.0343 -0.4816 -0.9803 -1.5198 -0.7442 -1.2696 -0.4877 1.0498 -1.6611 -2.1780 -0.7519 -0.0573 1.8737 -0.5333 0.6133 0.0551 -0.1731 0.9421 0.5260 0.4575 -1.6644 -0.2699 -0.3135 -0.4243 2.5099 -0.7946 -0.7059 -1.3087 0.9230 -0.5398 -1.0544 0.4877 1.8475 -0.0171 1.5016 0.6639 -2.0277 0.9118 -1.6037 0.1726 -0.2693 -1.6754 0.3091 -0.4914 0.4172 0.1470 1.5454 -0.9659 -0.3808 Columns 45 through 66 -0.2427 0.6006 0.7881 0.1452 -0.2661 0.8255 0.7855 0.2592 0.1963 0.0686 0.6204 -0.0825 -2.7009 0.5087 -0.7471 0.6285 0.8582 0.3182 0.4012 0.5414 -0.4635 -0.8297 -0.8376 -0.6327 -1.7207 0.2507 -2.2007 0.2470 -1.4210 0.9207 -0.9699 -0.1680 0.3564 -0.2788 -0.0111 0.4384 1.1022 -0.9073 -1.5230 -0.4111 0.9419 -0.8387 -1.1460 1.2086 2.0568 -0.2750 0.0769 0.0925 -0.0499 0.5940 -0.1154 -1.2031 -1.8675 -0.7579 -0.0921 -0.0010 0.6037 0.1765 0.0018 0.5480 -0.0300 1.3002 -0.3101 0.6420 -0.0104 -0.5437 1.0293 0.5553 0.2856 0.7490 0.7516 0.8262 1.4713 -0.3601 0.4403 0.8954 -0.6730 0.6993 -0.8034 0.7322 0.1828 0.6300 0.8411 0.8548 -0.5231 1.9126 -0.1628 1.8854 -1.4363 0.3748 -0.4122 0.0668 0.4038 3.2295 1.8983 -0.4170 -1.7229 -1.3898 -1.4574 -0.5795 -1.0599 -0.6753 0.7388 0.5745 0.4388 -0.2170 -0.0276 -1.1328 1.3301 -0.4120 1.1222 1.6037 -0.9076 -2.0337 -0.0425 0.9924 0.5339 -0.9943 0.4461 0.0593 0.4277 1.6696 -1.1686 -0.9353 -1.0913 0.4382 -0.5750 -0.1822 -1.5486 -1.3129 -0.7827 -0.4636 Columns 67 through 88 1.5375 -0.6172 0.7630 1.4582 0.4960 1.0441 0.1139 0.8479 0.8904 -0.0655 0.5224 0.3396 0.4863 0.4904 0.4259 -1.1700 0.9905 0.7595 0.4921 -0.0377 -0.7438 0.2945 -1.7675 0.0912 -0.7481 -1.8178 2.2909 -0.9219 -0.3296 1.4905 -0.5049 0.3704 -1.0991 0.0010 -0.5068 0.2767 0.0557 1.7974 -0.4277 -0.4161 -1.5677 0.3072 0.4226 1.2113 1.8291 0.4297 -0.0395 0.7116 -0.3161 0.2870 -1.6942 0.2283 -0.6939 1.9671 0.9813 1.2012 0.8428 -1.2425 0.2013 -0.9363 -0.5069 -1.0491 2.1922 -0.9870 1.2962 0.1695 0.6201 -2.1203 0.9199 -0.4952 0.5098 -0.3206 1.0467 0.4606 -1.3862 1.7105 -0.4254 -0.3584 -0.4277 0.2223 0.7250 0.5416 0.5245 -0.7529 1.0604 0.0063 -1.1043 -0.3598 0.3082 -1.2486 -0.6466 0.5877 0.8581 -1.6631 1.4379 0.2711 -0.9368 0.2604 -1.0279 -0.6419 1.3876 -0.7034 0.4954 -1.3502 -1.2316 0.3003 0.4692 0.0975 -0.8908 0.3189 0.8925 -1.0722 -0.4349 1.5501 -0.3177 0.9701 -0.7283 -0.0149 1.7196 -1.2338 -0.1987 0.3414 0.4321 0.4417 0.0913 -1.4343 0.2452 -1.1739 -1.5984 -1.4487 1.5659 -0.4672 Columns 89 through 100 -2.2876 0.8133 -1.3842 0.8663 -0.2993 0.5435 -0.5635 -0.8349 -1.1232 0.0470 0.6798 -0.5021 -1.8427 -1.1277 -0.4006 -0.6520 -1.3521 0.0526 -0.6934 0.7613 0.9519 -0.9534 -1.3110 -0.4334 0.1553 0.1519 1.0008 -0.0929 -1.4677 1.8407 -0.7559 0.2547 -0.5550 2.5657 -0.8447 -0.5390 -0.8529 0.1275 -0.2916 -0.5304 -0.4930 2.2017 -1.4175 0.2243 -1.2064 0.0160 1.1907 -0.2501 0.7980 -0.2192 0.0178 -1.2111 1.0719 -0.5097 1.7664 0.8944 -0.5343 -0.3339 -0.7654 -0.6763 -0.6831 -0.8456 0.1670 -0.5257 0.7193 -0.9483 -0.3974 0.0385 -2.3019 0.4462 -0.2525 -1.1134

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by