issue creating loop with matrix multiplication
Ältere Kommentare anzeigen
Hi. I'm quite new to Matlab, and have a project, and am stuck on trying to run a loop. I'm trying to solve for the lat/lon/height of a sonar system. I've been given the GPS Lat/Lon and pitch, roll, yaw and heave from the sensor, as well as their relative offsets. I've started with this, and can get it run successfully once , but can't get it to loop for all the records in my file (~500). thanks in advance.
for i = 1:length (roll)
matRotZ = [cos(heading(i)) -sin(heading(i)) 0; sin(heading(i)) cosd(heading(i)) 0; 0 0 1];
matRotY = [cos(pitch(i)) 0 sin(pitch(i)); 0 1 0; -sin(pitch(i)) 0 cosd(pitch(i))];
matRotX = [1 0 0; 0 cos(roll(i)) -sin(roll(i)); 0 sin(roll(i)) cosd(roll(i))];
% Tait-bryan matrix multiplication.
RotAll = matRotZ * matRotY * matRotX;
PosRotated = RotAll * [X; Y; Z];
% [X; Y ; Z] is the offset of the instrument from Reference Point.
end
Antworten (1)
What are you trying to save out of the loop? If you want to save the RotAll for each element of heading, pitch and roll, do:
RotAll{i} = matRotZ * matRotY * matRotX;
Of course you will then have to change the next line to:
PosRotated = RotAll{i} * [X; Y; Z];
4 Kommentare
Dana commented:
"Hi, I'd like RotAll, for each, but am ultimately interested in PosRotated... So, based on your input, I could do this?
RotAll(i) = matRotZ * matRotY * matRotX;
PosRotated(i) = RotAll * [X; Y; Z];
I shall give it a try, thank you. Dana"
No!
Look at the difference:
RotAll(i) % Your code
RotAll{i} % My code {}, not ()
The way I wrote it, your RotAll will be a cell array, which is how you store many different elements of different sizes. Look at this example:
C{1} = magic(2);
C{2} = [1 2 3];
C{3} = 4;
% Now examine the contents with:
C{1}
If you want to save PosRotated for each iteration also, you will have to make that a cell too.
Matt Fig
am 29 Sep. 2012
Dana comments (again as an Answer!):
"ok, I see, thanks. however, I'm getting an error when I use:
RotAll{i} = matRotZ * matRotY * matRotX;
I get the following Error:
Cell contents assignment to a non-cell array object. Error in EM_Matrix_New (line 21) RotAll{i} = matRotZ * matRotY * matRotX;"
Matt Fig
am 29 Sep. 2012
If you are getting that error than either you forgot to clear a RotAll from the workspace because you are running a script or there is more to your code than you showed because I get no such error just using your code snippet.
Kategorien
Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!