separation matrix in several matrices, "i" times

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

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?
Tom
Tom on 26 Sep 2012
Edited: Tom on 26 Sep 2012
i was previously defined in a for loop.
Matt Fig
Matt Fig on 26 Sep 2012
Edited: Matt Fig on 26 Sep 2012
Yes, it looks like A, B, etc will have only one non-zero element: A(dv), B(dv), etc, if length(x) is equal to length(y) (which it is in the above). Otherwise the code will error on subscripts being real...
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 — I was initially responding to Tom's comment. Seems you and I came to the same conclusion in reply while I was writing it.
Matt Fig
Matt Fig on 26 Sep 2012
Edited: Matt Fig on 26 Sep 2012
@SS, I see. We need instant updates for answers, don't we?
@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.
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.
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.

Sign in to comment.

Answers (5)

mirch
mirch on 26 Sep 2012
Edited: mirch on 26 Sep 2012
Yes .. I want it to be 3-dimensional!
I muss include a new variable, or just use (i)?
Or what i suppose to do, that i get 3-dimensional Jac?
Please help me with some code:)
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

Thank You ... but i need so much Jac matrix, how much is x length

Sign in to comment.

mirch
mirch on 27 Sep 2012
Edited: mirch on 27 Sep 2012
Now i did on this way:
%TEST IZRAČUNA MATRIKE
% dimenzije robota ACMA XR701%
a1 = 250;
d1 = 645;
a2 = 1000;
a3 = 230;
d2 = 1587.4;
% branje minimalne vrednosti
x_min = 2;
y_min = 3;
z_min = 4;
% skaliranje vrednosti
x_res = 1;
y_res = 1;
z_res = 1;
% branje maksimalne vrednosti
x_max = 5;
y_max = 6;
z_max = 7;
% grupiranje podatkov v točke
x = [x_min:x_res:x_max]';
y = [y_min:y_res:y_max]';
z = [z_min:z_res:z_max]';
% izračun notranjih koordinat
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 k1=1:dv
q1(k1) = atan2(y(k1),x(k1));
end
else
display('vektorja x in y nista enako dolga !!');
end
% izračun posameznik elementov Jacobijanove matrike
for k2 = 1:dv
A(k2) = (-d2*cos(q1(k2))*cos(q2)*sin(q3)-d2*cos(q1(k2))*sin(q2)*cos(q3)-a3*cos(q1(k2))*cos(q2)*cos(q3)+a3*cos(q1(k2))*sin(q2)*sin(q3)-a2*cos(q1(k2))*cos(q2)+a1*sin(q1(k2))); % "levi zgoraj"
B(k2) = (d2*sin(q1(k2))*sin(q2)*sin(q3)-d2*sin(q1(k2))*cos(q2)*cos(q3)+a3*sin(q1(k2))*sin(q2)*cos(q3)+a3*sin(q1(k2))*cos(q2)*sin(q3)+a2*sin(q1(k2))*sin(q2)+a1*cos(q1(k2))); % "sredina zgoraj"
C(k2) = (-d2*sin(q1(k2))*cos(q2)*cos(q3)+d2*sin(q1(k2))*sin(q2)*sin(q3)+a3*sin(q1(k2))*cos(q2)*sin(q3)+a3*sin(q1(k2))*sin(q2)*cos(q3)-a2*sin(q1(k2))*cos(q2)+a1*cos(q1(k2))); % "desno zgoraj"
D(k2) = (-d2*sin(q1(k2))*cos(q2)*sin(q3)-d2*sin(q1(k2))*sin(q2)*cos(q3)-a3*sin(q1(k2))*cos(q2)*cos(q3)+a3*sin(q1(k2))*sin(q2)*sin(q3)-a3*sin(q1(k2))*cos(q2)+a3*cos(q1(k2))); % "levi v sredini"
E(k2) = (-d2*cos(q1(k2))*sin(q2)*sin(q3)+d2*cos(q1(k2))*cos(q2)*cos(q3)-a3*cos(q1(k2))*sin(q2)*cos(q3)-a3*cos(q1(k2))*cos(q2)*sin(q3)-a3*cos(q1(k2))*sin(q2)+a3*sin(q1(k2))); % "sredina v sredini"
F(k2) = (d2*cos(q1(k2))*cos(q2)*cos(q3)-d2*cos(q1(k2))*sin(q2)*sin(q3)-a3*cos(q1(k2))*cos(q2)*sin(q3)-a3*cos(q1(k2))*sin(q2)*cos(q3)+a3*cos(q1(k2))*cos(q2)+a3*sin(q1(k2))); % "desni v sredini"
G(k2) = (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(k2) = (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(k2) = (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"
end
% izračun Jacobijanove matrike
for k3 = 1:dv
Jac(1,1,:) = A;
Jac(1,2,:) = B;
Jac(1,3,:) = C;
Jac(2,1,:) = D;
Jac(2,2,:) = E;
Jac(2,3,:) = F;
Jac(3,1,:) = G;
Jac(3,2,:) = H;
Jac(3,3,:) = I;
end
% zložitev Jacobijanove matrike
for k4 = 1:dv
Jac(:,:,(k4));
end
% izračun determinante matrike
for k5 = 1:dv
dJac(k5) = det(Jac(k5));
end
% izračun gibljivostnega indeksa
for k6 = 1:dv
w(k6) = sqrt(dJac(k6)*(Jac(k6)));
end
So i get so much of matrix, how much points i have (x,y,z)

1 Comment

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.

Sign in to comment.

mirch
mirch on 27 Sep 2012
  1. I deleted k3 loop
  2. This loop is because i want 3x3 matrix n times. Not all 3x3 matrices in one matrix. So i dont want 3x3x4 - just 3x3 n times.
  3. I want determinant of all this Jac matrices (3x3)
  4. then i calculate with Jac and determinant of Jac some indeks for every point (x,y,z)

5 Comments

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.
Thank you .. I will try
But then i have another problem. I like to plot all this points in one figure.
How to do this with scatter3?
Please help me
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.
w(k6) = sqrt(dJac(k6)*(Jac(k6)));
i have one error in my calculations ... Jac(k6) must be transpose Jac(k6).
I'm sorry I told so late
How I tranpose every Jac to tranpose(Jac)?
Please help
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.

Sign in to comment.

If We start again, because I doing all wrong.
I define only the cube size, and density of points. (x_min, x_res, x_max, y_min, ...)
Then for every kind of that point need calculation.
Then I get from this calculation w, which is point to point differend.
This w would then define the color of the point
So if I have cube 3 times 3 times 3 big, i should calculate 27 calculations for w.
How do I do this?
For now I have this: %TEST IZRAČUNA MATRIKE clear all % dimenzije robota ACMA XR701% a1 = 250; d1 = 645; a2 = 1000; a3 = 230; d2 = 1587.4;
% branje minimalne vrednosti
x_min = 1;
y_min = 2;
z_min = 3;
% skaliranje vrednosti
x_res = 1;
y_res = 1;
z_res = 1;
% branje maksimalne vrednosti
x_max = 3;
y_max = 4;
z_max = 5;
% grupiranje podatkov v točke
x = [x_min:x_res:x_max]';
y = [y_min:y_res:y_max]';
z = [z_min:z_res:z_max]';
% ustvarjanje mreže
[X, Y, Z] = meshgrid(x, y, z);
%scatter3 (X(:), Y(:), Z(:), 10)
% izračun notranjih koordinat
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);
q1 = atan2(Y,X);
[m,n]=size(q1);
%k1m = 1:m;
%k1n = 1:n;
JA = (-d2*cos(q1)*cos(q2)*sin(q3)-d2*cos(q1)*sin(q2)*cos(q3)-a3*cos(q1)*cos(q2)*cos(q3)+a3*cos(q1)*sin(q2)*sin(q3)-a2*cos(q1)*cos(q2)+a1*sin(q1)); % "levi zgoraj"
JB = (d2*sin(q1)*sin(q2)*sin(q3)-d2*sin(q1)*cos(q2)*cos(q3)+a3*sin(q1)*sin(q2)*cos(q3)+a3*sin(q1)*cos(q2)*sin(q3)+a2*sin(q1)*sin(q2)+a1*cos(q1)); % "sredina zgoraj"
JC = (-d2*sin(q1)*cos(q2)*cos(q3)+d2*sin(q1)*sin(q2)*sin(q3)+a3*sin(q1)*cos(q2)*sin(q3)+a3*sin(q1)*sin(q2)*cos(q3)-a2*sin(q1)*cos(q2)+a1*cos(q1)); % "desno zgoraj"
JD = (-d2*sin(q1)*cos(q2)*sin(q3)-d2*sin(q1)*sin(q2)*cos(q3)-a3*sin(q1)*cos(q2)*cos(q3)+a3*sin(q1)*sin(q2)*sin(q3)-a3*sin(q1)*cos(q2)+a3*cos(q1)); % "levi v sredini"
JE = (-d2*cos(q1)*sin(q2)*sin(q3)+d2*cos(q1)*cos(q2)*cos(q3)-a3*cos(q1)*sin(q2)*cos(q3)-a3*cos(q1)*cos(q2)*sin(q3)-a3*cos(q1)*sin(q2)+a3*sin(q1)); % "sredina v sredini"
JF = (d2*cos(q1)*cos(q2)*cos(q3)-d2*cos(q1)*sin(q2)*sin(q3)-a3*cos(q1)*cos(q2)*sin(q3)-a3*cos(q1)*sin(q2)*cos(q3)+a3*cos(q1)*cos(q2)+a3*sin(q1)); % "desni v sredini"
JG = (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"
JH = (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"
JI = (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"
so ... then I need for every point 'Jac' ... Jac = [JA, JB, JC; JD, JE, JF; JG, JH, JI]
Please help me to continue

Categories

Tags

Asked:

on 26 Sep 2012

Community Treasure Hunt

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

Start Hunting!