separation matrix in several matrices, "i" times
Show older comments
So, I want to calculate the matrix (JAC). All the results throw me in a single matrix, instead of for each point (x,y,z) of the new matrix.
So how would you do that for each of (x,y,z) a new matrix
Code: a1 = 250; d1 = 645; a2 = 1000; a3 = 230; d2 = 1587.4;
% inputing for GUI minimal
x_min = 2;
y_min = 3;
z_min = 4;
% inputing for GUI resolution
x_res = 1;
y_res = 1;
z_res = 1;
% inputing for GUI maximal
x_max = 4;
y_max = 5;
z_max = 6;
% vector
x = [x_min:x_res:x_max]';
y = [y_min:y_res:y_max]';
z = [z_min:z_res:z_max]';
% calculation of internal coordinates
S3 = sin(a3);
C3 = cos(a3);
B = d2*S3 - a3*C3;
SB = sqrt(-(B*B)+a3*a3+d2*d2);
q3 = atan2(a3,d2)-atan2(B, SB);
S2 = sin(a2);
C2 = cos(a2);
q2 = atan2(S2,C2);
if length(x)==length(y)
dv =length(x);
for i=1:dv
q1(i) = atan2(y(i),x(i));
end
else
display('vektorja x in y nista enako dolga !!');
end
% calculate Jac matrix
A(i) = (-d2*cos(q1(i))*cos(q2)*sin(q3)-d2*cos(q1(i))*sin(q2)*cos(q3)-a3*cos(q1(i))*cos(q2)*cos(q3)+a3*cos(q1(i))*sin(q2)*sin(q3)-a2*cos(q1(i))*cos(q2)+a1*sin(q1(i))); % "levi zgoraj"
B(i) = (d2*sin(q1(i))*sin(q2)*sin(q3)-d2*sin(q1(i))*cos(q2)*cos(q3)+a3*sin(q1(i))*sin(q2)*cos(q3)+a3*sin(q1(i))*cos(q2)*sin(q3)+a2*sin(q1(i))*sin(q2)+a1*cos(q1(i))); % "sredina zgoraj"
C(i) = (-d2*sin(q1(i))*cos(q2)*cos(q3)+d2*sin(q1(i))*sin(q2)*sin(q3)+a3*sin(q1(i))*cos(q2)*sin(q3)+a3*sin(q1(i))*sin(q2)*cos(q3)-a2*sin(q1(i))*cos(q2)+a1*cos(q1(i))); % "desno zgoraj"
D(i) = (-d2*sin(q1(i))*cos(q2)*sin(q3)-d2*sin(q1(i))*sin(q2)*cos(q3)-a3*sin(q1(i))*cos(q2)*cos(q3)+a3*sin(q1(i))*sin(q2)*sin(q3)-a3*sin(q1(i))*cos(q2)+a3*cos(q1(i))); % "levi v sredini"
E(i) = (-d2*cos(q1(i))*sin(q2)*sin(q3)+d2*cos(q1(i))*cos(q2)*cos(q3)-a3*cos(q1(i))*sin(q2)*cos(q3)-a3*cos(q1(i))*cos(q2)*sin(q3)-a3*cos(q1(i))*sin(q2)+a3*sin(q1(i))); % "sredina v sredini"
F(i) = (d2*cos(q1(i))*cos(q2)*cos(q3)-d2*cos(q1(i))*sin(q2)*sin(q3)-a3*cos(q1(i))*cos(q2)*sin(q3)-a3*cos(q1(i))*sin(q2)*cos(q3)+a3*cos(q1(i))*cos(q2)+a3*sin(q1(i))); % "desni v sredini"
G(i) = (d2*sin(q2)*sin(q3)+d2*cos(q2)*cos(q3)+a3*sin(q2)*cos(q3)+a3*cos(q2)*sin(q3)+a2*sin(q2)+d1); % "levi spodaj"
H(i) = (d2*cos(q2)*sin(q3)+d2*sin(q2)*cos(q3)+a3*cos(q2)*cos(q3)-a3*sin(q2)*sin(q3)+a2*cos(q2)+d1); % "sredinski spodaj"
I(i) = (d2*sin(q2)*cos(q3)+d2*cos(q2)*sin(q3)-a3*sin(q2)*sin(q3)+a3*cos(q2)*cos(q3)+a2*sin(q2)+d1); % "desni spodaj"
Jac = [A, B, C; D, E, F; G, H, I]
10 Comments
Star Strider
on 26 Sep 2012
Edited: Star Strider
on 26 Sep 2012
Something is missing in the code you posted. Where you calculate your Jac matrix, I do not see subscript ‘i’ or a ‘for’ loop defined anywhere.
What do you want your Jac matrix to be? Do you want it to be 3-dimensional?
Star Strider
on 26 Sep 2012
I saw that, but in this situation, i = dv (a constant) after the loop. Maybe I missed something, but I do not see a for loop that includes the Jac matrix element calculations.
Matt Fig
on 26 Sep 2012
Exactly!
Star Strider
on 26 Sep 2012
@Matt Fig — I was initially responding to Tom's comment. Seems you and I came to the same conclusion in reply while I was writing it.
Star Strider
on 26 Sep 2012
Edited: Star Strider
on 26 Sep 2012
@Matt — That would be nice!
A few days ago, I was in the process of answering a post during which the OP deleted it. When I clicked ‘Save’ it wouldn't, so I saved the text to Notepad and refreshed the page to find a link that told me to contact TMW, so I did. At the time I had no idea what the problem was. I then went back to the main ‘Answers’ page to find that post absent and a similar one by the same OP. I answered that one without incident. (TMW later responded to my e-mail and I told them what I thought the problem was. They agreed.)
The disadvantage is the unimaginable bandwidth requirement to provide dynamic updates, as well as not deleting answers and comments in progress. I'll leave this to the queueing theorists to solve.
Image Analyst
on 27 Sep 2012
That's happened to me to. What happens more often is when I get done typing and move my right hand over to the mouse and accidentally bump the "back" button on the side of the mouse with my thumb. Sometimes going forward won't recover the text you had typed in. For that reason, and for a reason like you described, you need to use Lazarus. http://getlazarus.com/ I can't tell you how many times it's saved me a ton of re-typing. Basically it remembers what you typed into the last several edit boxes so on any web page, you can resurrect any of those texts that you've typed by right-clicking and selecting the text you want to paste into the edit box.
Star Strider
on 27 Sep 2012
Thank you for the Lazarus reference. I'll look into it.
You comment also seems relevant to the current thread ‘What’s missing from MATLAB Central...’. I suggest you post a version of it there as well.
Answers (5)
Star Strider
on 26 Sep 2012
Edited: Star Strider
on 26 Sep 2012
First, I suggest not using i or j as variables. MATLAB uses them for its imaginary operators, and using them as variables in your code could be confusing if you also have complex numbers. (I use k1 for outer loops, k2 for the next level of inner loops, and so on. It works for me.)
Second, I am not certain what you are doing, but with that caution, I suggest you consider something like this:
for i = 1:something
A(i) = ...
.
.
.
I(i) = ...
end
Then, to create your Jac matrix:
Jac(1,1,:) = A;
Jac(1,2,:) = B;
.
.
.
Jac(3,3,:) = I;
That is probably the easiest (although not the neatest) way for you to create your Jac matrix. At least you do not have to re-write large parts of your code to create it that way.
I am guessing how you want to set up your Jac matrix. Change the code as necessary to do what you intend.
1 Comment
Star Strider
on 27 Sep 2012
1. You do not need the k3 loop. It does the same thing three times and the results do not change.
2. What do you want to do in your k4 loop? You are not doing anything with your Jac matrix here.
3. What do you want to do in you k5 loop? What parts of Jac do you want the determinant of? Your Jac matrix is [3 x 3 x 4]. Your reference to Jac(k5) is calling the single elements [Jac(1,1,1) Jac(2,1,1) Jac(3,1,1) Jac(1,2,1)] in order.
4. I do not know what you are doing in your k6 loop, but we will consider that after we get your k4 and k5 loops doing what you want them to do.
mirch
on 27 Sep 2012
0 votes
5 Comments
Star Strider
on 27 Sep 2012
Edited: Star Strider
on 27 Sep 2012
I suggest you keep Jac as a [3 x 3 x dv] matrix because it is easier to work with that way. (Here, dv = 4.)
Then calculate dJac this way:
for k5 = 1:dv
dJac(k5) = det(Jac(:,:,k5));
end
That will calculate the determinant of Jac for every value of dv.
Is that what you want to do?
If it is, for your k6 loop, since Jac is a matrix, I suggest you define w as:
for k6 = 1:dv
w(:,:,k6) = sqrt(dJac(k6)*Jac(:,:,k6));
end
Note that w is complex because some of the elements of Jac are negative.
mirch
on 30 Sep 2012
Star Strider
on 30 Sep 2012
Since w is complex, you have to take its absolute value in order to plot it. I suggest using plot3, as in this example:
absw = abs(w)
X = squeeze(absw(1,:,:))
Y = squeeze(absw(2,:,:))
Z = squeeze(absw(3,:,:))
figure(1)
plot3(X, Y, Z, '.-')
grid on
Experiment with various plots to get the result you want.
Note that scatter3 wants vectors, not matrices, to plot, so you would have to convert your matrices to vectors to use it.
mirch
on 2 Oct 2012
Star Strider
on 2 Oct 2012
Edited: Star Strider
on 2 Oct 2012
You cannot transpose a matrix with more than two dimensions, so you cannot simply transpose w since your w matrix is [3 x 3 x 4]. I do not know what in that situation you want to transpose, so I cannot give you specific advice. However, if you want to transpose every page of the 3rd dimension of w, I suggest:
for k6 = 1:dv
wt(:,:,k6) = w(:,:,k6)';
end
If you want to do something else, change that code appropriately.
Also, you have two options for transposing your w matrix, since it is complex.
The usual transpose operator (') is actually the complex conjugate transpose. So if you want the complex conjugate transpose, and we define wt as the transpose of w, do as I described in the previous loop.
If you want to do the transpose and not also do the complex conjugate operation, use the ‘dot-transpose’ operator (.') and do this:
for k6 = 1:dv
wt(:,:,k6) = w(:,:,k6).';
end
What you do depends on what you want to do with your wt matrix later.
mirch
on 2 Oct 2012
Categories
Find more on Creating and Concatenating Matrices 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!