how do I improve this code?

2 ビュー (過去 30 日間)
hadi mostafavi amjad
hadi mostafavi amjad 2019 年 8 月 23 日
編集済み: Bruno Luong 2019 年 8 月 23 日
hey matlabians, i have 2 vectors and i wanna inner product them arrey by arrey and write the result into the 3rd zeros vector. then i wanna add the 2nd arrey to 3rd arrey and write the result into the first arrey of 4th zeros vector, and add the 2nd one to 3rd one and to the 4th one and so on. i have a code but it's not sophisticated code, so i brought you my code and i wanna ask you if you have any better solution please help me. thanks a lot
clc,clear
i = 1;
x = [2 5 6 7 8];
y = [1 3 4 8 9];
z = zeros(1,10);
c = zeros(1,10);
m = 1;
for i =1:5
z(i) = (x(i).*y(i));
c(1) = z(1);
c(2) = z(m)+z(m+1);
c(3) = z(m)+z(m+1)+z(m+2);
c(4) = z(m)+z(m+1)+z(m+2)+z(m+3);
c(5) = z(m)+z(m+1)+z(m+2)+z(m+3)+z(m+4);
end

採用された回答

Bruno Luong
Bruno Luong 2019 年 8 月 23 日
編集済み: Bruno Luong 2019 年 8 月 23 日
x = [2 5 6 7 8];
y = [1 3 4 8 9];
z = x.*y;
z(10) = 0;
c = cumsum(z);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by