Multiply different sized arrays by cycling smaller array

조회 수: 4 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by