Multiply different sized arrays by cycling smaller array

4 次查看(过去 30 天)
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

采纳的回答

Manish
Manish 2024-10-15
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!

更多回答(1 个)

Voss
Voss 2024-10-15
编辑:Voss 2024-10-15
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>

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by