Multiply different sized arrays by cycling smaller array

3 ビュー (過去 30 日間)
Jack
Jack 2024 年 10 月 15 日
編集済み: Voss 2024 年 10 月 15 日
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 Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by