for loop with the same variable but two values

13 ビュー (過去 30 日間)
Eyad Alkhotani
Eyad Alkhotani 2022 年 2 月 18 日
回答済み: Arif Hoq 2022 年 2 月 18 日
I have a for loop which is the following
for i=1:2
bm(i) = bm + yi(i)*b1(i);
for j=1:2
aam(i,j)=aam+yi(i)*yi(j)*sqrt(aa(i)*aa(j));
end
end
for the yi and aa I have two values for each that I need to be multiplied to each other in each loop, and I'm not sure how exactly to do it.

回答 (2 件)

David Hill
David Hill 2022 年 2 月 18 日
No loop needed. Assumed yi and aa row vectors.
bm=bm+yi.*b1;
aam = aam+(yi'.*yi).*sqrt(aa'.*aa);

Arif Hoq
Arif Hoq 2022 年 2 月 18 日
if you want to use 'loop'
b1=5; % assuming value
yi=[2,3]; % assuming value
aa=[4,4]; % assuming value
bm=3; % assuming value
aam=4; % assuming value
C=cell(1,2);
for i=1
for j=2
C{1,i} = bm + yi(i)*b1;
C{1,j}=aam+yi(i)*yi(j)*sqrt(aa(i)*aa(j));
end
end
Output=[C{:}];

カテゴリ

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