How can I transfer the results of a four-cycle iteration into a matrix with a row for each iteration?

1 vue (au cours des 30 derniers jours)
Here is an example of my problem:
I have a Matrix- A (1x24) A[a1,1 a1,2 a1,3 ... a1,24 ], & a vector B[b1,1 b1,2 b1,3 ...b1,7]
i would to multiply each row of A by each element of B and obtain a Matrix C
C=[a11*b11 a12*b11 a13*b11 a14*b11;
a11*b21 a12*b21 a13*b21 a14*b21 a15*b21;
ecc........ ]
I tried this script, but i get only the last iteration as result.
A=[0.246 0.280 0.286 0.301 0.302 0.330 0.397 0.478 0.607 0.651 0.595 0.495 0.546 0.594 0.609 0.608 0.628 0.866 0.944 0.988 0.882 0.831 0.745 0.616];
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 i=1:7
C=A*B(i);
end
C
C = 1×24
0.1958 0.2229 0.2277 0.2396 0.2404 0.2627 0.3160 0.3805 0.4832 0.5182 0.4736 0.3940 0.4346 0.4728 0.4848 0.4840 0.4999 0.6893 0.7514 0.7864 0.7021 0.6615 0.5930 0.4903

Réponse acceptée

Karim
Karim le 21 Juin 2022
hello, if you want to use the for loop you need to index the rows
A=[0.246 0.280 0.286 0.301 0.302 0.330 0.397 0.478 0.607 0.651 0.595 0.495 0.546 0.594 0.609 0.608 0.628 0.866 0.944 0.988 0.882 0.831 0.745 0.616];
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];
C = zeros(size(B,1),size(A,2));
for i = 1:size(B,1)
C(i,:) = A * B(i);
end
% print the result
C
C = 12×24
0.2458 0.2797 0.2857 0.3007 0.3017 0.3297 0.3966 0.4775 0.6064 0.6503 0.5944 0.4945 0.5455 0.5934 0.6084 0.6074 0.6274 0.8651 0.9431 0.9870 0.8811 0.8302 0.7443 0.6154 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.2310 0.2629 0.2686 0.2826 0.2836 0.3099 0.3728 0.4488 0.5700 0.6113 0.5587 0.4648 0.5127 0.5578 0.5719 0.5709 0.5897 0.8132 0.8864 0.9277 0.8282 0.7803 0.6996 0.5784 0.2091 0.2380 0.2431 0.2558 0.2567 0.2805 0.3375 0.4063 0.5160 0.5534 0.5057 0.4208 0.4641 0.5049 0.5176 0.5168 0.5338 0.7361 0.8024 0.8398 0.7497 0.7063 0.6332 0.5236 0.1993 0.2268 0.2317 0.2438 0.2446 0.2673 0.3216 0.3872 0.4917 0.5273 0.4819 0.4010 0.4423 0.4811 0.4933 0.4925 0.5087 0.7015 0.7646 0.8003 0.7144 0.6731 0.6035 0.4990 0.1975 0.2248 0.2297 0.2417 0.2425 0.2650 0.3188 0.3838 0.4874 0.5228 0.4778 0.3975 0.4384 0.4770 0.4890 0.4882 0.5043 0.6954 0.7580 0.7934 0.7082 0.6673 0.5982 0.4946 0.1958 0.2229 0.2277 0.2396 0.2404 0.2627 0.3160 0.3805 0.4832 0.5182 0.4736 0.3940 0.4346 0.4728 0.4848 0.4840 0.4999 0.6893 0.7514 0.7864 0.7021 0.6615 0.5930 0.4903 0.1948 0.2218 0.2265 0.2384 0.2392 0.2614 0.3144 0.3786 0.4807 0.5156 0.4712 0.3920 0.4324 0.4704 0.4823 0.4815 0.4974 0.6859 0.7476 0.7825 0.6985 0.6582 0.5900 0.4879 0.2020 0.2299 0.2348 0.2471 0.2479 0.2709 0.3259 0.3924 0.4983 0.5345 0.4885 0.4064 0.4483 0.4877 0.5000 0.4992 0.5156 0.7110 0.7750 0.8111 0.7241 0.6823 0.6116 0.5057 0.2285 0.2601 0.2657 0.2796 0.2806 0.3066 0.3688 0.4441 0.5639 0.6048 0.5528 0.4599 0.5072 0.5518 0.5658 0.5648 0.5834 0.8045 0.8770 0.9179 0.8194 0.7720 0.6921 0.5723
alternatively, you can do it as a oneliner:
C2 = B .* A
C2 = 12×24
0.2458 0.2797 0.2857 0.3007 0.3017 0.3297 0.3966 0.4775 0.6064 0.6503 0.5944 0.4945 0.5455 0.5934 0.6084 0.6074 0.6274 0.8651 0.9431 0.9870 0.8811 0.8302 0.7443 0.6154 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.2310 0.2629 0.2686 0.2826 0.2836 0.3099 0.3728 0.4488 0.5700 0.6113 0.5587 0.4648 0.5127 0.5578 0.5719 0.5709 0.5897 0.8132 0.8864 0.9277 0.8282 0.7803 0.6996 0.5784 0.2091 0.2380 0.2431 0.2558 0.2567 0.2805 0.3375 0.4063 0.5160 0.5534 0.5057 0.4208 0.4641 0.5049 0.5176 0.5168 0.5338 0.7361 0.8024 0.8398 0.7497 0.7063 0.6332 0.5236 0.1993 0.2268 0.2317 0.2438 0.2446 0.2673 0.3216 0.3872 0.4917 0.5273 0.4819 0.4010 0.4423 0.4811 0.4933 0.4925 0.5087 0.7015 0.7646 0.8003 0.7144 0.6731 0.6035 0.4990 0.1975 0.2248 0.2297 0.2417 0.2425 0.2650 0.3188 0.3838 0.4874 0.5228 0.4778 0.3975 0.4384 0.4770 0.4890 0.4882 0.5043 0.6954 0.7580 0.7934 0.7082 0.6673 0.5982 0.4946 0.1958 0.2229 0.2277 0.2396 0.2404 0.2627 0.3160 0.3805 0.4832 0.5182 0.4736 0.3940 0.4346 0.4728 0.4848 0.4840 0.4999 0.6893 0.7514 0.7864 0.7021 0.6615 0.5930 0.4903 0.1948 0.2218 0.2265 0.2384 0.2392 0.2614 0.3144 0.3786 0.4807 0.5156 0.4712 0.3920 0.4324 0.4704 0.4823 0.4815 0.4974 0.6859 0.7476 0.7825 0.6985 0.6582 0.5900 0.4879 0.2020 0.2299 0.2348 0.2471 0.2479 0.2709 0.3259 0.3924 0.4983 0.5345 0.4885 0.4064 0.4483 0.4877 0.5000 0.4992 0.5156 0.7110 0.7750 0.8111 0.7241 0.6823 0.6116 0.5057 0.2285 0.2601 0.2657 0.2796 0.2806 0.3066 0.3688 0.4441 0.5639 0.6048 0.5528 0.4599 0.5072 0.5518 0.5658 0.5648 0.5834 0.8045 0.8770 0.9179 0.8194 0.7720 0.6921 0.5723
  3 commentaires
Pietro Fiondella
Pietro Fiondella le 21 Juin 2022
I mean that i wloud obtain a matrix E that should be with every row filled with the moltiplication between eac row af C and each element of D
A=[0.246 0.280 0.286 0.301 0.302 0.330 0.397 0.478 0.607 0.651 0.595 0.495 0.546 0.594 0.609 0.608 0.628 0.866 0.944 0.988 0.882 0.831 0.745 0.616];
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];
C = zeros(size(B,1),size(A,2));
for i = 1:size(B,1)
C(i,:) = A * B(i);
end
E=[c1,1*d11 c1,2*d1,1 c1,3*d1,1.....;
c1,1*d1,2 c1,2*d1,2 c1,3*d1,1......
............................................
c2,1*d1,1 c2,2*d1,1 c2,3*d1,1
c2,1*d2,1 c2,1*d2,1 c2,3*d2,1
and so on ]
I was able to do it only by this loop but i'm searching for an easyer way
D=[ 0.9990 0.9920 0.9390 0.8500 0.8100 0.8030 0.7960 0.7920 0.8210 0.9290 0.9900 1.0000];
for j=1:12;
E1=C*D(j);
cell_E1{j}=E1;
end
E=[cell_E1{1,1};cell_E1{1,2};cell_E1{1,3};cell_E1{1,4};cell_E1{1,5};cell_E1{1,6};cell_E1{1,7};cell_E1{1,8};cell_E1{1,9};cell_E1{1,10};cell_E1{1,11};cell_E1{1,12}];
A=cell_E1{1,1}
A = 12×24
0.2455 0.2794 0.2854 0.3004 0.3014 0.3293 0.3962 0.4770 0.6058 0.6497 0.5938 0.4940 0.5449 0.5928 0.6078 0.6068 0.6267 0.8643 0.9421 0.9860 0.8802 0.8293 0.7435 0.6148 0.2438 0.2775 0.2834 0.2983 0.2993 0.3270 0.3934 0.4737 0.6015 0.6451 0.5896 0.4905 0.5411 0.5887 0.6035 0.6025 0.6224 0.8582 0.9355 0.9791 0.8741 0.8235 0.7383 0.6105 0.2308 0.2627 0.2683 0.2824 0.2833 0.3096 0.3724 0.4484 0.5694 0.6107 0.5581 0.4643 0.5122 0.5572 0.5713 0.5703 0.5891 0.8124 0.8855 0.9268 0.8274 0.7795 0.6989 0.5778 0.2089 0.2378 0.2429 0.2556 0.2564 0.2802 0.3371 0.4059 0.5154 0.5528 0.5052 0.4203 0.4636 0.5044 0.5171 0.5163 0.5333 0.7354 0.8016 0.8390 0.7490 0.7056 0.6326 0.5231 0.1991 0.2266 0.2314 0.2436 0.2444 0.2670 0.3212 0.3868 0.4912 0.5268 0.4815 0.4005 0.4418 0.4807 0.4928 0.4920 0.5082 0.7008 0.7639 0.7995 0.7137 0.6724 0.6028 0.4985 0.1973 0.2246 0.2294 0.2415 0.2423 0.2647 0.3185 0.3835 0.4869 0.5222 0.4773 0.3971 0.4380 0.4765 0.4885 0.4877 0.5038 0.6947 0.7573 0.7926 0.7075 0.6666 0.5976 0.4942 0.1956 0.2227 0.2274 0.2394 0.2402 0.2624 0.3157 0.3801 0.4827 0.5177 0.4731 0.3936 0.4342 0.4724 0.4843 0.4835 0.4994 0.6886 0.7507 0.7857 0.7014 0.6608 0.5924 0.4898 0.1946 0.2215 0.2263 0.2382 0.2389 0.2611 0.3141 0.3782 0.4803 0.5151 0.4708 0.3916 0.4320 0.4700 0.4818 0.4811 0.4969 0.6852 0.7469 0.7817 0.6978 0.6575 0.5894 0.4874 0.2018 0.2297 0.2346 0.2469 0.2477 0.2707 0.3256 0.3920 0.4978 0.5339 0.4880 0.4060 0.4478 0.4872 0.4995 0.4987 0.5151 0.7103 0.7742 0.8103 0.7234 0.6816 0.6110 0.5052 0.2283 0.2599 0.2654 0.2793 0.2803 0.3063 0.3684 0.4436 0.5633 0.6042 0.5522 0.4594 0.5067 0.5513 0.5652 0.5643 0.5828 0.8037 0.8761 0.9169 0.8186 0.7712 0.6914 0.5717
Karim
Karim le 22 Juin 2022
yes you can find the code below, both using a for loop and as one liners.
A = [0.246 0.280 0.286 0.301 0.302 0.330 0.397 0.478 0.607 0.651 0.595 0.495 0.546 0.594 0.609 0.608 0.628 0.866 0.944 0.988 0.882 0.831 0.745 0.616];
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];
C = zeros(size(B,1),size(A,2));
for i = 1:numel(B)
C(i,:) = A * B(i);
end
D = [0.9990 0.9920 0.9390 0.8500 0.8100 0.8030 0.7960 0.7920 0.8210 0.9290 0.9900 1.0000];
E = zeros(numel(D)*size(C,1), size(C,2));
for i = 1:numel(D)
currIdx = (i-1)*size(C,1)+(1:size(C,1));
E(currIdx,:) = C*D(i);
end
E
E = 144×24
0.2455 0.2794 0.2854 0.3004 0.3014 0.3293 0.3962 0.4770 0.6058 0.6497 0.5938 0.4940 0.5449 0.5928 0.6078 0.6068 0.6267 0.8643 0.9421 0.9860 0.8802 0.8293 0.7435 0.6148 0.2438 0.2775 0.2834 0.2983 0.2993 0.3270 0.3934 0.4737 0.6015 0.6451 0.5896 0.4905 0.5411 0.5887 0.6035 0.6025 0.6224 0.8582 0.9355 0.9791 0.8741 0.8235 0.7383 0.6105 0.2308 0.2627 0.2683 0.2824 0.2833 0.3096 0.3724 0.4484 0.5694 0.6107 0.5581 0.4643 0.5122 0.5572 0.5713 0.5703 0.5891 0.8124 0.8855 0.9268 0.8274 0.7795 0.6989 0.5778 0.2089 0.2378 0.2429 0.2556 0.2564 0.2802 0.3371 0.4059 0.5154 0.5528 0.5052 0.4203 0.4636 0.5044 0.5171 0.5163 0.5333 0.7354 0.8016 0.8390 0.7490 0.7056 0.6326 0.5231 0.1991 0.2266 0.2314 0.2436 0.2444 0.2670 0.3212 0.3868 0.4912 0.5268 0.4815 0.4005 0.4418 0.4807 0.4928 0.4920 0.5082 0.7008 0.7639 0.7995 0.7137 0.6724 0.6028 0.4985 0.1973 0.2246 0.2294 0.2415 0.2423 0.2647 0.3185 0.3835 0.4869 0.5222 0.4773 0.3971 0.4380 0.4765 0.4885 0.4877 0.5038 0.6947 0.7573 0.7926 0.7075 0.6666 0.5976 0.4942 0.1956 0.2227 0.2274 0.2394 0.2402 0.2624 0.3157 0.3801 0.4827 0.5177 0.4731 0.3936 0.4342 0.4724 0.4843 0.4835 0.4994 0.6886 0.7507 0.7857 0.7014 0.6608 0.5924 0.4898 0.1946 0.2215 0.2263 0.2382 0.2389 0.2611 0.3141 0.3782 0.4803 0.5151 0.4708 0.3916 0.4320 0.4700 0.4818 0.4811 0.4969 0.6852 0.7469 0.7817 0.6978 0.6575 0.5894 0.4874 0.2018 0.2297 0.2346 0.2469 0.2477 0.2707 0.3256 0.3920 0.4978 0.5339 0.4880 0.4060 0.4478 0.4872 0.4995 0.4987 0.5151 0.7103 0.7742 0.8103 0.7234 0.6816 0.6110 0.5052 0.2283 0.2599 0.2654 0.2793 0.2803 0.3063 0.3684 0.4436 0.5633 0.6042 0.5522 0.4594 0.5067 0.5513 0.5652 0.5643 0.5828 0.8037 0.8761 0.9169 0.8186 0.7712 0.6914 0.5717
or as a one liner
C = B.*A;
E = kron(D',C)
E = 144×24
0.2455 0.2794 0.2854 0.3004 0.3014 0.3293 0.3962 0.4770 0.6058 0.6497 0.5938 0.4940 0.5449 0.5928 0.6078 0.6068 0.6267 0.8643 0.9421 0.9860 0.8802 0.8293 0.7435 0.6148 0.2438 0.2775 0.2834 0.2983 0.2993 0.3270 0.3934 0.4737 0.6015 0.6451 0.5896 0.4905 0.5411 0.5887 0.6035 0.6025 0.6224 0.8582 0.9355 0.9791 0.8741 0.8235 0.7383 0.6105 0.2308 0.2627 0.2683 0.2824 0.2833 0.3096 0.3724 0.4484 0.5694 0.6107 0.5581 0.4643 0.5122 0.5572 0.5713 0.5703 0.5891 0.8124 0.8855 0.9268 0.8274 0.7795 0.6989 0.5778 0.2089 0.2378 0.2429 0.2556 0.2564 0.2802 0.3371 0.4059 0.5154 0.5528 0.5052 0.4203 0.4636 0.5044 0.5171 0.5163 0.5333 0.7354 0.8016 0.8390 0.7490 0.7056 0.6326 0.5231 0.1991 0.2266 0.2314 0.2436 0.2444 0.2670 0.3212 0.3868 0.4912 0.5268 0.4815 0.4005 0.4418 0.4807 0.4928 0.4920 0.5082 0.7008 0.7639 0.7995 0.7137 0.6724 0.6028 0.4985 0.1973 0.2246 0.2294 0.2415 0.2423 0.2647 0.3185 0.3835 0.4869 0.5222 0.4773 0.3971 0.4380 0.4765 0.4885 0.4877 0.5038 0.6947 0.7573 0.7926 0.7075 0.6666 0.5976 0.4942 0.1956 0.2227 0.2274 0.2394 0.2402 0.2624 0.3157 0.3801 0.4827 0.5177 0.4731 0.3936 0.4342 0.4724 0.4843 0.4835 0.4994 0.6886 0.7507 0.7857 0.7014 0.6608 0.5924 0.4898 0.1946 0.2215 0.2263 0.2382 0.2389 0.2611 0.3141 0.3782 0.4803 0.5151 0.4708 0.3916 0.4320 0.4700 0.4818 0.4811 0.4969 0.6852 0.7469 0.7817 0.6978 0.6575 0.5894 0.4874 0.2018 0.2297 0.2346 0.2469 0.2477 0.2707 0.3256 0.3920 0.4978 0.5339 0.4880 0.4060 0.4478 0.4872 0.4995 0.4987 0.5151 0.7103 0.7742 0.8103 0.7234 0.6816 0.6110 0.5052 0.2283 0.2599 0.2654 0.2793 0.2803 0.3063 0.3684 0.4436 0.5633 0.6042 0.5522 0.4594 0.5067 0.5513 0.5652 0.5643 0.5828 0.8037 0.8761 0.9169 0.8186 0.7712 0.6914 0.5717

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by