How to obtain a matrix from the iterations of a for loop?

1 view (last 30 days)
I woud like to obtain a matrix C[84x24] with every row filled, by the moltiplication between each row af a Matrix A[7x24] by each element of vector B[1x12].
I am able to do it only by this loop with "cell command at the end but i'm looking for an easyer way .
A=[ 0.2167 0.2467 0.2520 0.2652 0.2661 0.2907 0.3498 0.4211 0.5348 0.5735 0.5242 0.4361 0.4810 0.5233 0.5365 0.5356 0.5533 0.7629 0.8317 0.8704 0.7770 0.7321 0.6563 0.5427
0.2406 0.2738 0.2797 0.2944 0.2954 0.3227 0.3883 0.4675 0.5936 0.6367 0.5819 0.4841 0.5340 0.5809 0.5956 0.5946 0.6142 0.8469 0.9232 0.9663 0.8626 0.8127 0.7286 0.6024
0.2440 0.2778 0.2837 0.2986 0.2996 0.3274 0.3938 0.4742 0.6021 0.6458 0.5902 0.4910 0.5416 0.5892 0.6041 0.6031 0.6230 0.8591 0.9364 0.9801 0.8749 0.8244 0.7390 0.6111
0.2453 0.2792 0.2851 0.3001 0.3011 0.3290 0.3958 0.4766 0.6052 0.6490 0.5932 0.4935 0.5444 0.5922 0.6072 0.6062 0.6261 0.8634 0.9412 0.9850 0.8794 0.8285 0.7428 0.6142
0.2453 0.2792 0.2851 0.3001 0.3011 0.3290 0.3958 0.4766 0.6052 0.6490 0.5932 0.4935 0.5444 0.5922 0.6072 0.6062 0.6261 0.8634 0.9412 0.9850 0.8794 0.8285 0.7428 0.6142
0.2448 0.2786 0.2846 0.2995 0.3005 0.3284 0.3950 0.4756 0.6040 0.6477 0.5920 0.4925 0.5433 0.5910 0.6060 0.6050 0.6249 0.8617 0.9393 0.9831 0.8776 0.8268 0.7413 0.6129
0.2369 0.2696 0.2754 0.2899 0.2908 0.3178 0.3823 0.4603 0.5845 0.6269 0.5730 0.4767 0.5258 0.5720 0.5865 0.5855 0.6048 0.8340 0.9091 0.9514 0.8494 0.8003 0.7174 0.5932];
B=[0.999; 0.992; 0.939; 0.850; 0.810; 0.803; 0.796; 0.792; 0.821; 0.929; 0.990; 1.000];
for j=1:12;
C=A*B(j);
cell_C{j}=C;
end
D=[cell_C{1,1};cell_C{1,2};cell_C{1,3};cell_C{1,4};cell_C{1,5};cell_C{1,6};cell_C{1,7};cell_C{1,8};cell_C{1,9};cell_C{1,10};cell_C{1,11};cell_C{1,12}];
D
D = 84×24
0.2165 0.2465 0.2517 0.2649 0.2658 0.2904 0.3495 0.4207 0.5343 0.5729 0.5237 0.4357 0.4805 0.5228 0.5360 0.5351 0.5527 0.7621 0.8309 0.8695 0.7762 0.7314 0.6556 0.5422 0.2404 0.2735 0.2794 0.2941 0.2951 0.3224 0.3879 0.4670 0.5930 0.6361 0.5813 0.4836 0.5335 0.5803 0.5950 0.5940 0.6136 0.8461 0.9223 0.9653 0.8617 0.8119 0.7279 0.6018 0.2438 0.2775 0.2834 0.2983 0.2993 0.3271 0.3934 0.4737 0.6015 0.6452 0.5896 0.4905 0.5411 0.5886 0.6035 0.6025 0.6224 0.8582 0.9355 0.9791 0.8740 0.8236 0.7383 0.6105 0.2451 0.2789 0.2848 0.2998 0.3008 0.3287 0.3954 0.4761 0.6046 0.6484 0.5926 0.4930 0.5439 0.5916 0.6066 0.6056 0.6255 0.8625 0.9403 0.9840 0.8785 0.8277 0.7421 0.6136 0.2451 0.2789 0.2848 0.2998 0.3008 0.3287 0.3954 0.4761 0.6046 0.6484 0.5926 0.4930 0.5439 0.5916 0.6066 0.6056 0.6255 0.8625 0.9403 0.9840 0.8785 0.8277 0.7421 0.6136 0.2446 0.2783 0.2843 0.2992 0.3002 0.3281 0.3946 0.4751 0.6034 0.6471 0.5914 0.4920 0.5428 0.5904 0.6054 0.6044 0.6243 0.8608 0.9384 0.9821 0.8767 0.8260 0.7406 0.6123 0.2367 0.2693 0.2751 0.2896 0.2905 0.3175 0.3819 0.4598 0.5839 0.6263 0.5724 0.4762 0.5253 0.5714 0.5859 0.5849 0.6042 0.8332 0.9082 0.9504 0.8486 0.7995 0.7167 0.5926 0.2150 0.2447 0.2500 0.2631 0.2640 0.2884 0.3470 0.4177 0.5305 0.5689 0.5200 0.4326 0.4772 0.5191 0.5322 0.5313 0.5489 0.7568 0.8250 0.8634 0.7708 0.7262 0.6510 0.5384 0.2387 0.2716 0.2775 0.2920 0.2930 0.3201 0.3852 0.4638 0.5889 0.6316 0.5772 0.4802 0.5297 0.5763 0.5908 0.5898 0.6093 0.8401 0.9158 0.9586 0.8557 0.8062 0.7228 0.5976 0.2420 0.2756 0.2814 0.2962 0.2972 0.3248 0.3906 0.4704 0.5973 0.6406 0.5855 0.4871 0.5373 0.5845 0.5993 0.5983 0.6180 0.8522 0.9289 0.9723 0.8679 0.8178 0.7331 0.6062

Accepted Answer

Voss
Voss on 21 Jun 2022
Edited: Voss on 21 Jun 2022
Use kron.
A=[ 0.2167 0.2467 0.2520 0.2652 0.2661 0.2907 0.3498 0.4211 0.5348 0.5735 0.5242 0.4361 0.4810 0.5233 0.5365 0.5356 0.5533 0.7629 0.8317 0.8704 0.7770 0.7321 0.6563 0.5427
0.2406 0.2738 0.2797 0.2944 0.2954 0.3227 0.3883 0.4675 0.5936 0.6367 0.5819 0.4841 0.5340 0.5809 0.5956 0.5946 0.6142 0.8469 0.9232 0.9663 0.8626 0.8127 0.7286 0.6024
0.2440 0.2778 0.2837 0.2986 0.2996 0.3274 0.3938 0.4742 0.6021 0.6458 0.5902 0.4910 0.5416 0.5892 0.6041 0.6031 0.6230 0.8591 0.9364 0.9801 0.8749 0.8244 0.7390 0.6111
0.2453 0.2792 0.2851 0.3001 0.3011 0.3290 0.3958 0.4766 0.6052 0.6490 0.5932 0.4935 0.5444 0.5922 0.6072 0.6062 0.6261 0.8634 0.9412 0.9850 0.8794 0.8285 0.7428 0.6142
0.2453 0.2792 0.2851 0.3001 0.3011 0.3290 0.3958 0.4766 0.6052 0.6490 0.5932 0.4935 0.5444 0.5922 0.6072 0.6062 0.6261 0.8634 0.9412 0.9850 0.8794 0.8285 0.7428 0.6142
0.2448 0.2786 0.2846 0.2995 0.3005 0.3284 0.3950 0.4756 0.6040 0.6477 0.5920 0.4925 0.5433 0.5910 0.6060 0.6050 0.6249 0.8617 0.9393 0.9831 0.8776 0.8268 0.7413 0.6129
0.2369 0.2696 0.2754 0.2899 0.2908 0.3178 0.3823 0.4603 0.5845 0.6269 0.5730 0.4767 0.5258 0.5720 0.5865 0.5855 0.6048 0.8340 0.9091 0.9514 0.8494 0.8003 0.7174 0.5932];
B=[0.999; 0.992; 0.939; 0.850; 0.810; 0.803; 0.796; 0.792; 0.821; 0.929; 0.990; 1.000];
kron method:
D_test = kron(B,A)
D_test = 84×24
0.2165 0.2465 0.2517 0.2649 0.2658 0.2904 0.3495 0.4207 0.5343 0.5729 0.5237 0.4357 0.4805 0.5228 0.5360 0.5351 0.5527 0.7621 0.8309 0.8695 0.7762 0.7314 0.6556 0.5422 0.2404 0.2735 0.2794 0.2941 0.2951 0.3224 0.3879 0.4670 0.5930 0.6361 0.5813 0.4836 0.5335 0.5803 0.5950 0.5940 0.6136 0.8461 0.9223 0.9653 0.8617 0.8119 0.7279 0.6018 0.2438 0.2775 0.2834 0.2983 0.2993 0.3271 0.3934 0.4737 0.6015 0.6452 0.5896 0.4905 0.5411 0.5886 0.6035 0.6025 0.6224 0.8582 0.9355 0.9791 0.8740 0.8236 0.7383 0.6105 0.2451 0.2789 0.2848 0.2998 0.3008 0.3287 0.3954 0.4761 0.6046 0.6484 0.5926 0.4930 0.5439 0.5916 0.6066 0.6056 0.6255 0.8625 0.9403 0.9840 0.8785 0.8277 0.7421 0.6136 0.2451 0.2789 0.2848 0.2998 0.3008 0.3287 0.3954 0.4761 0.6046 0.6484 0.5926 0.4930 0.5439 0.5916 0.6066 0.6056 0.6255 0.8625 0.9403 0.9840 0.8785 0.8277 0.7421 0.6136 0.2446 0.2783 0.2843 0.2992 0.3002 0.3281 0.3946 0.4751 0.6034 0.6471 0.5914 0.4920 0.5428 0.5904 0.6054 0.6044 0.6243 0.8608 0.9384 0.9821 0.8767 0.8260 0.7406 0.6123 0.2367 0.2693 0.2751 0.2896 0.2905 0.3175 0.3819 0.4598 0.5839 0.6263 0.5724 0.4762 0.5253 0.5714 0.5859 0.5849 0.6042 0.8332 0.9082 0.9504 0.8486 0.7995 0.7167 0.5926 0.2150 0.2447 0.2500 0.2631 0.2640 0.2884 0.3470 0.4177 0.5305 0.5689 0.5200 0.4326 0.4772 0.5191 0.5322 0.5313 0.5489 0.7568 0.8250 0.8634 0.7708 0.7262 0.6510 0.5384 0.2387 0.2716 0.2775 0.2920 0.2930 0.3201 0.3852 0.4638 0.5889 0.6316 0.5772 0.4802 0.5297 0.5763 0.5908 0.5898 0.6093 0.8401 0.9158 0.9586 0.8557 0.8062 0.7228 0.5976 0.2420 0.2756 0.2814 0.2962 0.2972 0.3248 0.3906 0.4704 0.5973 0.6406 0.5855 0.4871 0.5373 0.5845 0.5993 0.5983 0.6180 0.8522 0.9289 0.9723 0.8679 0.8178 0.7331 0.6062
Compare to the cell array method:
for j=1:12;
C=A*B(j);
cell_C{j}=C;
end
D=vertcat(cell_C{:});
The result is the same:
isequal(D_test,D)
ans = logical
1

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!