Multiply different sized arrays by cycling smaller array

4 visualizzazioni (ultimi 30 giorni)
a = 1,2,3
b = 4,5,6,7,8
c=a*b
I want c=a*b in the form:
C(a=1), C(a=2), C(a=3)
where c is three seperate 1x5 arrayss
I am envisaging a for loop cycling through array(a) but can't get it to work

Risposta accettata

Manish
Manish il 15 Ott 2024
Hi,
I understand that you want to create three separate 1x5 arrays, denoted as C, using the arrays ‘a’ and ‘b’.
Here is the code Implementation:
a = [1, 2, 3];
b = [4, 5, 6, 7, 8];
C = cell(1, length(a));
for i = 1:length(a)
% Multiply the current element of a with the entire array b
C{i} = a(i) * b;
end
for i = 1:length(C)
fprintf('C(a=%d) = ', a(i));
disp(C{i});
end
C(a=1) =
4 5 6 7 8
C(a=2) =
8 10 12 14 16
C(a=3) =
12 15 18 21 24
Hope this solves!

Più risposte (1)

Voss
Voss il 15 Ott 2024
Modificato: Voss il 15 Ott 2024
Perhaps one of these is what you describe:
a = [1,2,3];
b = [4,5,6,7,8];
c = a.'.*b
c = 3×5
4 5 6 7 8 8 10 12 14 16 12 15 18 21 24
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
c = a.*b.'
c = 5×3
4 8 12 5 10 15 6 12 18 7 14 21 8 16 24
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
c = reshape(c,1,[])
c = 1×15
4 5 6 7 8 8 10 12 14 16 12 15 18 21 24
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by