Group maths on for loop

I am trying to solve this
omegas=0:1:10;
RR=40:70;
d=90;
for i=1:length(omegas)
for c=1:length(RR)
gg=d+(omegas.*RR);
end
end
for every value of omegas, I want to calculate gg. The idea is that gg should be a 31 by 11 matrix. Means that each omegas is calculated with all values of RR

5 comentarios

Stephen23
Stephen23 el 8 de Ag. de 2019
DARLINGTON ETAJE 's "Answer" moved here:
Guys...nice try...it's still not working...maybe because my question excluded something. see the real code below
Thet=5:10005;
Theta=Thet';
R=15;
r=5;
epsilon=100;
delta=(R-r)*(epsilon/100);
for omega_b1=1:9
for t=1:10001
x_b1(omega_b1,t)=(delta.*cos(t'.*omega_b1))+(r.*cos(t'.*Theta));
y_b1(omega_b1,t)=(delta.*sin(t'.*omega_b1))-(r.*sin(t'.*Theta));
Vb_x1(omega_b1,t)=- (r.*Theta).*sin(t'.*Theta) - ((((R - r)*epsilon).*omega_b1).*sin(t'.*omega_b1))/100;
Vb_y1(omega_b1,t)=- (r.*Theta).*cos(t'.*Theta) + ((((R - r)*epsilon).*omega_b1).*cos(t'.*omega_b1))/100;
ab_x1(omega_b1,t)=- (r.*(Theta.^2)).*cos(t'.*Theta) - ((((R - r)*epsilon).*(omega_b1^2)).*cos(t'.*omega_b1))/100;
ab_y1(omega_b1,t)=(r.*(Theta.^2)).*sin(t'.*Theta) -((((R - r)*epsilon).*(omega_b1^2)).*sin(t'.*omega_b1))/100;
end
end
Guillaume
Guillaume el 8 de Ag. de 2019
Since Theta is a vector, each expression, such as:
delta.*cos(t'.*omega_b1) + r.*cos(t'.*Theta)
is a vector. This can't be stuffed into the scalar x_b1(omega_b1,t). So, it's not clear what you're trying to calculate. Maybe x1 should be a function of Theta as well? (i.e. 3D)
It's also unclear why t is transposed (t' everywhere), since t is scalar. The transpose doesn't do anything.
Perhaps you started with code which had no loops (where the transpose would have made sense) but if you don't give us the right starting point, it's hard to help you.
DARLINGTON ETAJE
DARLINGTON ETAJE el 8 de Ag. de 2019
I was just trying to show that each value of t has a corresponding Theta. Both are 10001 in length. I just want to get answers for all the variables in the left in the format of 9 by 10001
qq=8:10008;
for m = 1:9
for n = 1:10001
A(m, n) = (m+n)+qq;
end
end
this is an easier way of looking at the problem
Guillaume
Guillaume el 8 de Ag. de 2019
this is an easier way of looking at the problem
Not really, it's exactly the same issue. qq is a vector, so you still have a vector that you're trying to store in a scalar.
However, I've understood what you're asking from your description. Sometimes, explaining in words is better than broken code.

Iniciar sesión para comentar.

Respuestas (2)

Adam
Adam el 8 de Ag. de 2019
Editada: Adam el 8 de Ag. de 2019

0 votos

omegas=0:1:10;
RR=40:70;
d=90;
numOmegas = numel( omegas );
numRR = numel( RR );
gg = zeros( numOmegas, numRR );
for i=1:numOmegas
for c=1:numRR
gg(i,c) =d+(omegas(i).*RR(c));
end
end
You could do it perfectly fine without a loop at all though too probably.

1 comentario

DARLINGTON ETAJE
DARLINGTON ETAJE el 8 de Ag. de 2019
please the codes with the real problem below

Iniciar sesión para comentar.

Guillaume
Guillaume el 8 de Ag. de 2019
Editada: Guillaume el 8 de Ag. de 2019

0 votos

The vectorised version of what you intended with your loop (but didn't achieve):
R=15;
r=5;
epsilon=100;
delta=(R-r)*(epsilon/100);
Theta = 5:10005; %a ROW vector
omega_b1 = (1:9)'; %a COLUMN vector. The two vectors must be along different dimensions
x_b1 = delta*cos(omega_b1) + r*cos(Theta); %automatic expansion of compatible arrays
y_b1 = delta*sin(omega_b1) - r*sin(Theta);
Vb_x1 = -r*Theta.*sin(Theta) - (R - r)*epsilon*omega_b1.*sin(omega_b1)/100;
Vb_y1 = -r*Theta.*cos(Theta) + (R - r)*epsilon*omega_b1.*cos(omega_b1)/100;
ab_x1 = -r*Theta.^2.*cos(Theta) - (R - r)*epsilon*omega_b1.^2.*cos(omega_b1)/100;
ab_y1 = r*Theta.^2.*sin(Theta) - (R - r)*epsilon*omega_b1.^2.*sin(omega_b1)/100;
I've removed a lot of unnecessary brackets.
For how this works, see compatible array sizes

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Ag. de 2019

Editada:

el 8 de Ag. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by