Debugging 24 symmetric operators - matlab

I have written the following code to calculate the disorientation between two points in a large dataset using 24 crystal symmetry operators. The code seems to work fine though the end result is not right. I have a huge dataset containing the euler angles for each points. I find the misorientation between 1st point and 2nd point and then between 2 and 3rd and so on.
I want the end result to contain the corresponding misorientaion angles for each two data points. Following is the full code for your better understanding of my requirement.
LA=[phi1 phi phi2];
function gi=get_gi(pvec)
%pvec is a 1x3 vector of [phi1 phi phi2]
g_11=((cosd(pvec(1)).*cosd(pvec(3)))-(sind(pvec(1)).*sind(pvec(3)).*cosd(pvec(2))));
g_12=((sind(pvec(1)).*cosd(pvec(3)))+(cosd(pvec(1)).*sind(pvec(3)).*cosd(pvec(2))));
g_13= (sind(pvec(3)).*sind(pvec(2)));
g_21 =((-cosd(pvec(1)).*sind(pvec(3)))-(sind(pvec(1)).*cos(pvec(3)).*cos(pvec(2))));
g_22 = ((-sin(pvec(1)).*sind(pvec(3)))+(cosd(pvec(1)).*cosd(pvec(3)).*cosd(pvec(2))));
g_23 = (cosd(pvec(3)).*sind(pvec(2)));
g_31 = (sind(pvec(1)).* sind(pvec(2)));
g_32 = -cosd(pvec(1)).* sind(pvec(2));
g_33 = cosd(pvec(2));
gi =[g_11 g_12 g_13;g_21 g_22 g_23;g_31 g_32 g_33];
f = [1 1 1 -1 1 -1 -1 -1 1 1 -1 -1];
l= [1 1 1];
for i=1:3:10
l1= [f(i) 0 0;0 f(i+1) 0;0 0 f(i+2)];
l2= [0 f(i) 0;0 0 f(i+1);f(i+2) 0 0];
l3= [0 0 f(i);f(i+1) 0 0;0 f(i+2) 0];
l4= -[0 0 f(i);0 f(i+1) 0;f(i+2) 0 0];
l5= -[0 f(i) 0;f(i+1) 0 0;0 0 f(i+2)];
l6= -[f(i) 0 0;0 0 f(i+1);0 f(i+2) 0];
l=[l;l1;l2;l3;l4;l5;l6];
end
k=1;
t=1;
for m=1:(length(a)-1)
for i=2:3:71
for j=2:3:71
g_r= (get_gi(LA(m,:)*l(i:i+2,1:3))*(inv(get_gi(LA(m+1,:)))*inv(l(j:j+2,1:3))));
tr(k)= g_r(1,1) + g_r(2,2) +g_r(3,3);
angle(k) = real(acosd((tr(k)-1)/2));
k=k+1;
end
angle(angle==0)=360;
del_theta=min(angle)
del(t)=del_theta;
t=t+1;
end

Related

How to draw weighted convex hull in Matlab

Suppose I have a Matlab array PE of size Ex1. The elements of PE are between 0 and 1 and they sum up to one.
Take another array PEY of size ExY. The elements of PEY are one or zero. Moreover, for each row, there exists at least a one. Y=3.
For example
clear
E=4;
Y=3;
PE=[1/2; 1/6; 1/6; 1/6];
PEY=[1 0 0; 0 1 1; 1 1 1; 0 1 1];
Now, consider the simplex with vertices (1,0,0), (0,1,0), and (0,0,1)
patch([0 0 1],[0 1 0],[1 0 0],[0.8 0.8 0.8]);
axis equal
axis([0 1 0 1 0 1])
view(120,30)
I want to draw a convex subset A of such simplex. A is constructed as follows.
STEP 1: We construct an Ex1 cell array PEY_expanded such that, for each e-th row of PEY that has more than a 1, we write down all admissible 1xY vectors containing just a 1 and stack them in PEY_expanded{e}.
PEY_expanded=cell(E,1);
for e=1:E
if isequal(PEY(e,:),[1 1 1])
PEY_expanded{e}=[1 0 0; 0 1 0; 0 0 1];
elseif isequal(PEY(e,:),[1 1 0])
PEY_expanded{e}=[1 0 0; 0 1 0];
elseif isequal(PEY(e,:),[1 0 1])
PEY_expanded{e}=[1 0 0; 0 0 1];
elseif isequal(PEY(e,:),[0 1 1])
PEY_expanded{e}=[0 1 0; 0 0 1];
else
PEY_expanded{e}=PEY(e,:);
end
end
STEP 2: Take Cartesian product PEY_expanded{1} x PEY_expanded{2} x ... PEY_expanded{E} and get PEY_cartesian.
Note: the code below is specific for E=4 and not general
size_PEY_expanded=zeros(E,1);
for e=1:E
size_PEY_expanded(e)=size(PEY_expanded{e},1);
end
[a,b,c,d]=ndgrid(1: size_PEY_expanded(1),1: size_PEY_expanded(2),...
1: size_PEY_expanded(3), 1: size_PEY_expanded(4));
PEY_Cartesian= [PEY_expanded{1}(a,:),PEY_expanded{2}(b,:),...
PEY_expanded{3}(c,:), PEY_expanded{4}(d,:)];
PEY_Cartesian_rearranged=cell(prod(size_PEY_expanded),1);
for i=1:prod(size_PEY_expanded)
PEY_Cartesian_rearranged{i}=[PEY_Cartesian(i,1:3); PEY_Cartesian(i,4:6);...
PEY_Cartesian(i,7:9); PEY_Cartesian(i,10:end)];
end
PEY_Cartesian=PEY_Cartesian_rearranged;
STEP 3: For each possible cell of PEY_Cartesian, for y=1,...,Y, weight PEY_Cartesian{i}(e,y) by PE(e) and then sum across e.
PY=zeros(prod(size_PEY_expanded),Y);
for i=1:prod(size_PEY_expanded)
for y=1:Y
temp=0;
for e=1:E
temp=temp+PE(e)*PEY_Cartesian{i}(e,y);
end
PY(i,y)=temp;
end
end
STEP 4: Draw the region A that is the convex hull of the rows of PY (black region in the picture)
%Need https://fr.mathworks.com/matlabcentral/fileexchange/37004-suite-of-functions-to-perform-uniform-sampling-of-a-sphere
close all
patch([0 0 1],[0 1 0],[1 0 0],[0.8 0.8 0.8]);
axis equal
axis([0 1 0 1 0 1])
view(120,30)
hold on
T = delaunayTriangulation(PY);
K = convexHull(T);
patch('Faces',K,'Vertices',T.Points,'FaceColor','k','edgecolor','k');
hold on
QUESTION:
The algorithm above is unfeasible for large E. In my actual case I have E=216, for example. In particular, step 2 is unfeasible.
Could you suggest an easier way to proceed? Given that A is a convex region, maybe there is some shortcut I'm unable to see.

randomly select histogram data MATLAB

I have an input 2D histogram that I want to do 2-fold cross-validation with. The problem is I don't know how to extract two mutually exclusive random samples of the data from a histogram. If it was a couple of lists of the positional information of each data point, that would be easy - shuffle the data in the lists in the same way, and split the lists equally.
So for a list I would do this:
list1 = [1,2,3,3,5,6,1];
list2 = [1,3,6,6,5,2,1];
idx = randperm(length(list1)); % ie. idx = [4 3 1 5 6 2 7]
shlist1 = list1(idx); % shlist1 = [3,3,1,5,6,2,1]
shlist2 = list2(idx); % shlist2 = [6,6,1,5,2,3,1]
slist1 = shlist1(1:3); % slist1 = [3,3,1]
elist1 = shlist1(4:6); % elist1 = [5,6,2,1]
slist2 = shlist2(1:3); % slist2 = [6,6,1]
elist2 = shlist2(4:6); % elist2 = [5,2,3,1]
But if this same data was presented to me as a histogram
hist = [2 0 0 0 0 0]
[0 0 0 0 0 1]
[0 1 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 1 0]
[0 0 2 0 0 0]
I want the result to be something like this
hist1 = [0 0 0 0 0 0]
[0 0 0 0 0 1]
[0 1 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 1 0 0 0]
hist2 = [2 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 1 0]
[0 0 1 0 0 0]
so that different halves of the data are randomly, and equally assigned to two new histograms.
Would this be equivalent to taking a random integer height of each bin hist(i,j), and adding that to the equivalent bin in hist1(i,j), and the difference to hist2(i,j)?
% hist as shown above
hist1 = zeros(6);
hist2 = zeros(6);
for i = 1:length(hist(:,1))*length(hist(1,:))
randNum = rand;
hist1(i) = round(hist(i)*randNum);
hist2(i) = hist(i) - hist1(i);
end
And if that is equivalent, is there a better way/built-in way of doing it?
My actual histogram is 300x300 bins, and contains about 6,000,000 data points, and it needs to be fast.
Thanks for any help :)
EDIT:
The suggested bit of code I made is not equivalent to taking a random sample of positional points from a list, as it does not maintain the overall probability density function of the data.
Halving the histograms should be fine for my 6,000,000 points, but I was hoping for a method that would still work for few points.
You can use rand or randi to generate two histograms. The first method is more efficient however the second is more random.
h = [[2 0 0 0 0 0]
[0 0 0 0 0 1]
[0 1 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 1 0]
[0 0 2 0 0 0]];
%using rand
h1 = round(rand(size(h)).*h);
h2 = h - h1;
%using randi
h1 = zeros(size(h));
for k = 1:numel(h)
h1(k) = randi([0 h(k)]);
end
h2 = h - h1;
Suppose H is your 2D histogram. The following code extracts a single random index with a probability proportional to the count at that index - which I think is what you want.
cc = cumsum(H(:));
if cc(1) ~= 0
cc = [0; cc];
end
m = cc(end);
ix = find(cc > m*rand, 1);
To extract multiple samples, you need to write your own find function (preferably a binary search for efficiency) that extracts some n number of samples in one call. This will give you a vector of indices (call it ix_vec) chosen with probability proportional to the Histogram count at each index.
Then if we denote by X the numerical values corresponding to each location in the Histogram, your random sample is:
R1 = X(ix_vec);
Repeat for the second random sample set.

Why does my 3 axes system coordinate orientation change x with y values?

I am using Matlab and Euler Angles in order to reorient a 3axes coordinate system. Specifically,
Rz = [cos(ψ) sin(ψ) 0;-sin(ψ) cos(ψ) 0;0 0 1];
Ry = [cos(φ) 0 -sin(φ);0 1 0;sin(φ) 0 cos(φ)];
Rx = [1 0 0;0 cos(θ) -sin(θ);0 sin(θ) cos(θ)];
Rtotal = Rz*Ry*Rz
Then I loop through my old system coordinates (x,y,z) and make a vector coord_old. Then I get the reoriented system with (xn,yn,zn)
for i=1:size(num,1)
coord_old = [x(i,1);y(i,1);z(i,1)];
coord_new = Rtotal*coord_old;
xn(i,1) = coord_new(1,1);
yn(i,1) = coord_new(2,1);
zn(i,1) = coord_new(3,1);
end
My issue is that when θ,φ,ψ≃0 then x->-y and y->x and when θ,φ≃0 and ψ=90 then x and y will not rotate! That means that when x,y should rotate they don't and when they shouldn't rotate they stay as they were!
--EDIT--
For example, when ψ=20.0871, φ=0.0580 and θ=0.0088 I get these results
See that x->-y and y->x while z doesn't change at all!
Any thoughts?
Ok, I see two main problems here:
Rtotal = Rz*Ry*Rz is probably not what you want since Rz is multiplied twice. I think you mean Rtotal = Rz*Ry*Rx.
Your rotation matrix seems to be incorrect. Check this Wikipedia artice to get the correct signs.
Here a corrected rotation matrix:
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1];
Ry = [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rx;
With this matrix I get the correct results:
x=1; y=2; z=3;
psi=0; phi=0; theta=0;
[xn,yn,zn] >> 1 2 3
x=1; y=2; z=3;
psi=90/180*pi; phi=0; theta=0;
[xn,yn,zn] >> -2 1 3
And here a full graphical example of a cube in 3d-space:
% Create cube (not in origin)
DVert = [0 0 0; 0 1 0; 1 1 0; 1 0 0 ; ...
0 0 1; 0 1 1; 1 1 1; 1 0 1];
DSide = [1 2 3 4; 2 6 7 3; 4 3 7 8; ...
1 5 8 4; 1 2 6 5; 5 6 7 8];
DCol = [0 0 1; 0 0.33 1; 0 0.66 1; ...
0 1 0.33; 0 1 0.66; 0 1 1];
% Rotation angles
psi = 20 /180*pi; % Z
phi = 45 /180*pi; % Y
theta = 0 /180*pi; % X
% Rotation matrix
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1];
Ry = [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rz;
% Apply rotation
DVertNew = Rtotal * DVert';
% Plot cubes
figure;
patch('Faces',DSide,'Vertices',DVert,'FaceColor','flat','FaceVertexCData',DCol);
patch('Faces',DSide,'Vertices',DVertNew','FaceColor','flat','FaceVertexCData',DCol);
% Customize view
grid on;
axis equal;
view(30,30);
When I use your code and insert 0 for all angles, I get Rtotal:
Rtotal =
1 0 0
0 1 0
0 0 1
This is the identity matrix and will not change your values.
You have an error in your matrix multiplication. I think you should multiply: Rtotal*coord_old. I think you are missing the _old. depending on what is in you coordvariable, this may be the bug.
When I run:
for i=1:size(1,1)
coord_old = [1;2;3];
coord_new = Rtotal*coord_old;
xn(i,1) = coord_new(1,1);
yn(i,1) = coord_new(2,1);
zn(i,1) = coord_new(3,1);
end
I get the correct result:
coord_new =
1
2
3
Thank you both #Steffen and #Matt. Unfortunately, my reputation is not high enough to vote Up your answers!
The problem was not with Rtotal as #Matt correctly stated. It should be as it was Rz*Ry*Rx. However, both your ideas helped me test my code with simple examples (5 sets of coordinates and right hand rule), and realize where my (amateur) mistake was.
I had forgotten I had erased parts of codes where I was expressing my angles to degrees... I should be using sind & cosd instead of sin and cos.

For loops to take specific values only in MATLAB

I have a MATLAB program which uses two for loops to iterate up to 5 times. However, I want MATLAB to use only (1 1), (2 2), (3 3) and so on.
Here is the program:
syms l
a = [0 1 0 0 1 0;1 1 1 0 1 1;1 0 0 0 1 1;1 1 1 0 0 1;0 1 1 0 1 1];
n = [2 1;1 1;1 1;1 1;2 1];
for l = 1:5
for i = 1:5
j = n(l,1);
if a(i,j) == 0
a(i,j:end) = circshift(a(i,j:end),[n(l,2) n(l,2)]);
for j = n(l,1):n(l,1)+n(l,2)
a(i,n(l,1)) = 1;
end
else
a(i,j:end) = circshift(a(i,j:end),[n(l,2) n(l,2)]);
for j = n(l,1):n(l,1)+n(l,2)
a(i,n(l,1)) = 0;
end
end
break;
end
break;
end
I want MATLAB my program to work like this:
first l = 1 and i = 1;
second l = 2 and i = 2;
third l = 3 and i = 3;
fourth l = 4 and i = 4;
and so on...
#RodyOldenhuis basically answered your question, and I'm flattered he hasn't made this into an actual answer!
Simply take your code and change the inner for loop index so that it matches the outer loop index. In other words, do this:
syms l %// Why are you doing this?
a = [0 1 0 0 1 0;1 1 1 0 1 1;1 0 0 0 1 1;1 1 1 0 0 1;0 1 1 0 1 1];
n = [2 1;1 1;1 1;1 1;2 1];
for l = 1:5
i = l; %// Change here - Remove second for loop
%// ... [rest of your code here]
%// Get rid of the break in this loop
end
%// Removed break here too
end

3 dimensional matrices

I have a 3D 4-by-4-by-4 matrix and I consider it circular (so a 4th element of row closes with the 1st and same goes for columns and pages).
we know that in 3D each point has exactly 26 neighbors which can be stated as (i, j, k-1) (i, j, k+1) etc. but I am not sure how to make matlab know that a (i,j, k-1) neighbor of a point (1,1,1) is not (1,1,0) but (as it is circular) (1,1,4) as well as that point (2,4,3)'s neighbor (i,j+1,k) is not (2,5,3) but (2,1,3). In other words HOW DO I MAKE IT CIRCULAR?
Thank you
MATLAB does not have built-in facilities for this, but you can use the mod (modulus) function when accessing your matrix to achieve the effect you want. To illustrate this on a vector:
v=[1 2 3];
i=5;
result=v(mod(i-1, length(v))+1);
% assigns 2 to 'result'
You'll probably want to write a function that encapsulates the "circular" matrix access so that you have to do the index computations in only one place.
The idea is to use the MOD function as #MartinB explained. Here some code to efficiently compute the neighbors of each point in your 4x4x4 cube:
%# generate X/Y/Z coordinates of each point of the 4x4x4 cube
sz = [4 4 4]; %# size of the cube along each dimension
[X Y Z] = ndgrid(1:sz(1),1:sz(2),1:sz(3));
coords = [X(:) Y(:) Z(:)];
%# generate increments to get the 26 neighbors around a 3D point
[X Y Z] = ndgrid([-1 0 1], [-1 0 1], [-1 0 1]);
nb = [X(:) Y(:) Z(:)];
nb(ismember(nb,[0 0 0],'rows'),:) = []; %# remove the row [0 0 0]
%# for each 3D point, compute its neighbors
allNeighbors = zeros([size(nb,1) 3 size(coords,1)]);
szMod = repmat(sz, [size(nb,1) 1]);
for i=1:size(coords,1)
cc = bsxfun(#plus, nb, coords(i,:)); %# find 26 neighbors of coords(i,:)
cc = mod(cc-1,szMod)+1; %# wrap around circularly
allNeighbors(:,:,i) = cc; %# save them for later processing
end
The order of the neighbors generated is as follows:
>> nb
nb =
-1 -1 -1 %# read as: (i-1,j-1,k-1)
0 -1 -1 %# read as: (i,j-1,k-1)
1 -1 -1 %# ...
-1 0 -1
0 0 -1
1 0 -1
-1 1 -1
0 1 -1
1 1 -1
-1 -1 0
0 -1 0
1 -1 0
-1 0 0
1 0 0
-1 1 0
0 1 0
1 1 0
-1 -1 1
0 -1 1
1 -1 1
-1 0 1
0 0 1
1 0 1
-1 1 1
0 1 1
1 1 1