Matrices question

7 views (last 30 days)
Nathan Edge
Nathan Edge on 5 Oct 2011
Hi,
I have a script with two 'for' loops. Both of the loops involve matrices generated with 'linspace'-the first has N increments, and the second has P increments.
In the first loop, I have generated variables of the type A(N), B(N), etc. For the second loop, I want to craete a new variable of the type F(P,N). I am looking for F to be a matrix of P rows and N columns (or vise versa): however, I seem to be getting F to be a matrix of just one column. Does anybody have any advice?
EDIT: Sorry about the confusion, I'll post the code:
for Phi=linspace(Phimin, Phimax, N)
G(N)=Phi + BOC;
C2(N)=Rcon ./ ((Ccon .* cos(G(N))) + (Scon .* sin(G(N))));
C1(N)=Rmax^2 + (C2(N) .* cos(G(N) + Thetamax));
A(N)= (sqrt(abs(C1(N)+C2(N))) + sqrt(abs(C1(N)-C2(N))))./2;
B(N)=C2(N) ./ (2.*A(N));
for Theta=linspace(Thetamin, Thetamax, P)
F(N,P)= abs(((R(P).*(M*g*L)) .*cos(Theta)) /((A(N).*B(N)) .* sin(G(N)+Theta)));
disp(F(N,P))
end
end
BOC, Rcon. Ccon and Scon are all constants.
[EDITED, JSimon, 06-Oct-2011 09:27 UTC: Code formatted]
  2 Comments
Jan
Jan on 5 Oct 2011
Without seeing the code, only wild guessing is possible.
Please, Nathan, post the code instead of describing it.
Sean de Wolski
Sean de Wolski on 5 Oct 2011
Well my attempt at a wild guess. You overwrite the first column on each iteration
for ii ...
for kk ...
d(1,:) = f(ii,kk)
end
end

Sign in to comment.

Accepted Answer

Jan
Jan on 6 Oct 2011
You are writing to the element with index N and P, but their values are static and not effected by the loop. I assume, you want this:
vN = linspace(Phimin, Phimax, N)
for iN = 1:length(vN)
Phi = vN(iN);
G(iN)=Phi + BOC;
C2(iN)=Rcon ./ ((Ccon .* cos(G(iN))) + (Scon .* sin(G(iN))));
C1(iN)=Rmax^2 + (C2(iN) .* cos(G(iN) + Thetamax));
A(iN)= (sqrt(abs(C1(iN)+C2(iN))) + sqrt(abs(C1(iN)-C2(iN))))./2;
B(iN)=C2(iN) ./ (2.*A(iN));
vP = linspace(Thetamin, Thetamax, P);
for iP = 1:length(vP)
Theta = vP(iP);
F(iN,iP)= abs(((R(iP).*(M*g*L)) .*cos(Theta)) / ...
((A(iN).*B(iN)) .* sin(G(iN)+Theta)));
disp(F(iN,iP))
end
end
If this solves your problem, it can be made much faster: 1. Pre-allocate the vectors by zeros(1, length(vN)) and zeros(length(vN), length(vP)). And move all repeated calculation before the loops. E.g. the creation of vP, M*g*L.*cos(Theta) etc. But I'm not going to optimize your code, as long as I do not know, if it matchs your needs at all.
  2 Comments
Nathan Edge
Nathan Edge on 7 Oct 2011
Thank you very much for your answer, but I'm still struggling slightly. Can I just ask, is it possible for MATLAB to take a matrix like this:
X= 1
2
3
4
5
6...
And turn it into this:
Y= 1 2
3 4
5 6...
-where I can specify the number of rows and columns?
Andrei Bobrov
Andrei Bobrov on 7 Oct 2011
doc reshape
y = reshape(X,2,[])'

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!