Multiply different sized arrays by cycling smaller array

4 visualizaciones (últimos 30 días)
Jack
Jack el 15 de Oct. de 2024
Editada: Voss el 15 de Oct. de 2024
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

Respuesta aceptada

Manish
Manish el 15 de Oct. de 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!

Más respuestas (1)

Voss
Voss el 15 de Oct. de 2024
Editada: Voss el 15 de Oct. de 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>

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by