how to solve the expression using the loops

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 Kommentare

Jan
Jan am 14 Dez. 2017
What are P and Q, A and B? What's wrong with p*qr*b+p*br*q ?
phoenix
phoenix am 14 Dez. 2017
Bearbeitet: phoenix am 14 Dez. 2017
matrices.if i write in this form p*qr*b+p*br*q ,is it right? how can i combine these two different loops into a single one?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 14 Dez. 2017
Bearbeitet: Jan am 16 Dez. 2017

0 Stimmen

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 Kommentare

phoenix
phoenix am 16 Dez. 2017
It is not working.Any other suggestion? I do not require the elementwise product.
Jan
Jan am 16 Dez. 2017
Bearbeitet: Jan am 16 Dez. 2017
@Phoenix: I cannot run your code, because I do not have your inputs. "It does not work" does not allow to reconsider, what's going on, when you run it. It is not clear, what the problem with "combining" is, because it seems to be trivial to join the bodies of the two independent loops. If you need further help, please explain, what the problem is now.
But when I guess:
P = rand(5, 1);
Q = rand(5, 1);
A = rand(5, 1);
B = rand(5, 1);
p=0; q=0; pr=0; qr=0;
a=0; b=0; ar=0; br=0;
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);
end
for x = 1:5
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
R1 = p * qr * b + p * br * q
c = 5 .^ (0:4).';
p = sum(P(1:5, 1));
q = sum(Q(1:5, 1));
pr = sum(P(1:5, 1) .* c);
qr = sum(Q(1:5, 1) .* c);
R2 = p * qr * b + p * br * q
Now R1 and R2 have the same values. Do you want something else?
But a,b,ar,br do not occur in the wanted formula at all. Strange.
phoenix
phoenix am 17 Dez. 2017
Bearbeitet: phoenix am 17 Dez. 2017
Second loop is different.it ranges from 1:15.can i combine them? Leave aside/delete the term pr,a,ar. I just need to combine using for loop.
Jan
Jan am 17 Dez. 2017
@phoenix: Ah, sorry, I've overseen the difference of "5" and "15". Now I see it.
If one loop runs from 1 to 5 and the other from 1 to 15, what does "combining" them mean? I cannot guess, what you want to achieve then. Try to explain, what the actual problem is and why you want to use loops at all. Why not using the sum() commands as shown above?
phoenix
phoenix am 18 Dez. 2017
@Jan Simon: What does this symbol .' stand for used in c = 5 .^ (0:4).';?
Jan
Jan am 18 Dez. 2017
Bearbeitet: Jan am 18 Dez. 2017
@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

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 14 Dez. 2017

Bearbeitet:

Jan
am 18 Dez. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by