how to solve the expression using the loops
1 回表示 (過去 30 日間)
古いコメントを表示
p=0;
q=0;
pr=0;
qr=0;
a=0;
b=0;
ar=0;
br=0;
for x=1:1:5
p=p+P(x,1);
q=q+Q(x,1);
pr=pr+P(x,1)*5^(x-1);
qr=qr+Q(x,1)*5^(x-1);
end
for x=1:1:15
a=a+A(x,1);
b=b+B(x,1);
ar=ar+A(x,1)*5^(x-1);
br=br+B(x,1)*5^(x-1);
end
Now,how to find this: p*qr*b+p*br*q; combining the two loops.
2 件のコメント
回答 (1 件)
Jan
2017 年 12 月 14 日
編集済み: Jan
2017 年 12 月 16 日
What about:
for x=1:5
p = p + P(x,1);
q = q + Q(x,1);
pr = pr + P(x,1)*5^(x-1);
qr = qr + Q(x,1)*5^(x-1);
a = a + A(x,1);
b = b + B(x,1);
ar = ar + A(x,1)*5^(x-1);
br = br + B(x,1)*5^(x-1);
end
"Combining" seems to be easy, because the two loops are independent from each other. You could even omit the loops:
c = 5 .^ (0:4).'; % [EDITED] transposed
p = sum(P(1:5, 1));
q = sum(Q(1:5, 1)); % [EDITED] trailing parenthesis added
pr = sum(P(1:5,1) .* c);
qr = sum(Q(1:5,1) .* c);
And the same for the 2nd loop.
If p*qr*b+p*br*q is correct or not depends on what you want as output. Simply try it. Maybe you want the elementwise products:
p .* qr .* b + p .* br .* q
6 件のコメント
Jan
2017 年 12 月 18 日
編集済み: Jan
2017 年 12 月 18 日
@phoenix: .' is the transpose operator. It is frequently confused with ' , but this is the complex conjugate transposing. See https://www.mathworks.com/help/matlab/ref/transpose.html and https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!