the summation to be written in a for loop is here
TCPC = csvread ( 'tcpc.csv' ) ;
TCPC ( : , 3) = TCPC ( : , 3 ) * (100000); %unit conversion
TCPC ( : , 5) = TCPC ( : , 5 ) * (1e-6); %unit conversion
%------------------------------------------------------------------------
% Within the outer loop we calculate Bii's Psat for all components
for i = 1 : n
W(i,i) = TCPC (SP(i),1) ;
TC(i,i) = TCPC (SP(i),2);
PC(i,i) = TCPC (SP(i),3);
ZC(i,i) = TCPC (SP(i),4);
VC(i,i) = TCPC (SP(i),5);
TR(i,i) = T/TC(i,i);
Bi(i,i) = (R*TC(i,i)/PC(i,i))*((.083-(.422/(TR(i,i)^(1.6))))+(W(i,i)*(.139-
((.0172/(TR(i,i)^4.2))))));
phisat(i) = exp((Bi(i,i)*P)/(R*T)) ;
for j = i+1 : n % Within the inner loop we calculate Bij's (mixed coefficients)
W(i,j) = (TCPC (SP(i),1) + TCPC (SP(j),1)) / 2 ;
TC(i,j) = sqrt((TCPC (SP(i),2) * (TCPC (SP(j),2)))) ;
ZC(i,j) = ((TCPC (SP(i),4) * (TCPC (SP(j),4))))/2 ;
VC(i,j) = (((TCPC (SP(i),5)^(1/3) * (TCPC (SP(j),5)^(1/3))))/2)^(3) ;
PC(i,j) = ((ZC(i,j) * R * (TC(i,j)))/VC(i,j)) ;
Bi(i,j) = ((R*TC(i,j))/PC(i,j))*((.083-(.422/(TR(i,i)^(1.6))))+(W(i,j)*(.139-
((.0172/(TR(i,i)^4.2))))));
Bi(j,i)=Bi(i,j); % as Bij=Bji we write the inner loop for j = i+1 : n and assigned
the values to rest
end
end
for i = 1 : n
B(i,i) = ((X(i))^2)*Bi(i,i);
a = sum(B(i,i));
for j = i+1 : n
B(i,j) = X(i)*X(j)*Bi(i,j);
B(j,i) = B(i,j);
b = sum(B(i,j)); c = sum(B(j,I));
end
end
Bfinal = a+b+c
%------------------------------------------------------------------------
% Having calculated all the second virial coefficients,
% we calculate the fugacity coefficients for all the components of the mixture
% use phi bar 1-3 and phi 1-3
for i = 1:n
for j = i+1 : n
phi(i,j) = exp((((-Bfinal+2*X(i)Bi(i) + 2X(j)))P)/(RT));
end
end
Related
*[I found that the error occurred at line 57, z2 = w12.a1 + b12; with incompatible array size,
May I know how to fix it? Basically the data is from the simulation on MATLAB Simulink, so the size of the array changes as we increase the inputs from the original code which only have 1 input type][1]
%% load training data (use training_data.mat attached file)
% initialize number of nodes, weights, biases, errors and gradients.
%epochs and mini-batch size
clc
data = [out.dutycycle out.voltage out.current]
sample=1000;
labels = data(1:sample,1);
y = labels'; %output vector
images = data(1:sample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;
images = images'; %Input vectors
hn1 = 80; %Number of neurons in the first hidden layer
hn2 = 60; %Number of neurons in the second hidden layer
%Initializing weights and biases
w12 = randn(hn1,1000).*sqrt(2/1000);
w23 = randn(hn2,hn1)*sqrt(2/hn1);
w34 = randn(1,hn2)*sqrt(2/hn2);
b12 = randn(hn1,1);
b23 = randn(hn2,1);
b34 = randn(1,1);
%learning rate
eta = 0.0058;
%Initializing errors and gradients
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
epochs = 50;
m = 10; %Minibatch size
%% Training phase
for k = 1:epochs %Outer epoch loop
batches = 1;
for j = 1:sample/m
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
for i = batches:batches+m-1 %Loop over each minibatch
%Feed forward
a1 = images(:,i);
z2 = w12.*a1 + b12;
a2 = elu(z2);
z3 = w23*a2 + b23;
a3 = elu(z3);
z4 = w34*a3 + b34;
a4 = elu(z4); %Output vector
%backpropagation
error4 = (a4-y(:,i)).*elup(z4);
error3 = (w34'*error4).*elup(z3);
error2 = (w23'*error3).*elup(z2);
errortot4 = errortot4 + error4;
errortot3 = errortot3 + error3;
errortot2 = errortot2 + error2;
grad4 = grad4 + error4*a3';
grad3 = grad3 + error3*a2';
grad2 = grad2 + error2*a1';
end
%Gradient descent
w34 = w34 - eta/m*grad4;
w23 = w23 - eta/m*grad3;
w12 = w12 - eta/m*grad2;
b34 = b34 - eta/m*errortot4;
b23 = b23 - eta/m*errortot3;
b12 = b12 - eta/m*errortot2;
batches = batches + m;
end
fprintf('Epochs:');
disp(k) %Track number of epochs
[images,y] = shuffle(images,y); %Shuffles order of the images for next epoch
end
disp('Training done!')
%note : create folder for this experiment and save the parameters.
% don't forget to keep the folder in matlab path!
save('wfour.mat','w34');
save('wthree.mat','w23');
save('wtwo.mat','w12');
save('bfour.mat','b34');
save('bthree.mat','b23');
save('btwo.mat','b12');
%% Testing phase
% load testing data with labels ... (use testing_data.mat attached file)
testsample = 100;
labels = test(1:testsample,1);
y = labels';
images = test(1:testsample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;%copy
images = images';
we34 = matfile('wfour.mat');
w4 = we34.w34;
we23 = matfile('wthree.mat');
w3 = we23.w23;
we12 = matfile('wtwo.mat');
w2 = we12.w12;
bi34 = matfile('bfour.mat');
b4 = bi34.b34;
bi23 = matfile('bthree.mat');
b3 = bi23.b23;
bi12 = matfile('btwo.mat');
b2 = bi12.b12;
success = 0;
n = testsample;
for i = 1:n
out2 = elu(w2*images(:,i)+b2);
out3 = elu(w3*out2+b3);
out = elu(w4*out3+b4);
big = 0;
num = 0;
for k = 1:10
if out(k) > big
num = k-1;
big = out(k);
end
end
if labels(i) == num
success = success + 1;
end
end
fprintf('Accuracy: ');
fprintf('%f',success/n*100);
disp(' %');
%% ELU activation function
function fr = elu(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = x(i);
else
f(i) = 0.2*(exp(x(i))-1);
end
end
fr = f;
end
%% derivative of the ELU activation function
function fr = elup(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = 1;
else
f(i) = 0.2*exp(x(i));
end
end
fr = f;
end
%% SHuffle function
function [B,v] = shuffle(A,y)
cols = size(A,2);
P = randperm(cols);
B = A(:,P);
v = y(:,P);
end
Your help is much appreciated, thank you
MATLAB Error :
Arrays have incompatible sizes for this operation.
Error in ANN_PV_Array (line 57)
z2 = w12.*a1 + b12;
J1 = 18.041;
J2 = 1.99;
J3 = 6.58;
J4 = 1.46;
J5 = 7.50;
J6 = 15.46;
J7 = 4.21;
J8 = 7.68;
J9 = 24.84;
J10 = 8.58;
J11 = 6.57;
J12 = 4.20;
J13 = 4.63;
J14 = 0.7648;
J15 = 0.3607;
J16 = 68.48;
Js1 = 145.09;
Js2 = 128.58;
Js3 = 138.841;
Js4 = 145.09;
Jcar = 1.7916;
Jhollow = 1.478;
r1 = 45;
r2 = 46;
r3 = 38;
r4 = 15;
r5 = 37;
r6 = 68;
r7 = 29;
r8 = 37;
r9 = 60;
r10 = 33;
r11 = 29;
r12 = 27;
r13 = 52;
r14 = 19;
r15 = 16;
r16 = 57;
k0 = 241.32;
w2 = 1000;
kcar = 0.356;
khollow = 6.091;
D = 4867.91;
%syms h3(t) x3(t) theta2(t) theta1(t) x2(t) h4(t) h5(t) h1(t) h2(t) x1(t)
%ode1 = diff(h3)- (J4*r9*(diff(theta2,2)))/r4 == (theta2*khollow*r9)/r4 - kcar*theta1;
%ode2 = diff(x3,t) - (h3/J8)*r8 + w2*r3 == 0;
%ode3 = diff(x2,t) - (h2/J10)*r10 + w2*r5 == 0;
%ode4 = diff(theta2,t)*(r4- 1/(k0*r4)) - (diff(theta2,t,3)/(k0*r4)) - h3*r9/(J9 + Js1) + w2*r4 == 0;
%ode5 = diff(h1,t) - D*r16*(diff(x1)) == k0*x1*r16 - D*r16*((h4*r14)/(Js2 + J1 + (Jcar/3)));
%ode6 = diff(x1,t) - (h4)*r14/(Js2 + J1 + (Jcar/3)) + h1*r16/J16 == 0;
%ode7 = diff(h5,t) - x3*k0*r8 == 0;
%ode8 = diff(h2,t) - x2*k0*r10 == 0;
%ode9 = diff(h4,t) - kcar*theta1 - (2*h4*D)/(Js2 + J1 + (Jcar/3)) + k0*x1*r14 == 0;
%ode10 = diff(theta1,t) - h3/(J9 + Js1) + (h4)/(Js2 + J1 + (Jcar/3)) == 0;
%odes = [ode1; ode2; ode3; ode4; ode5; ode6; ode7; ode8; ode9; ode10];
%S = dsolve(odes);
f = #(t,x) [(J4*r9*(diff(x(4),2)))/r4 + (x(4)*khollow*r9)/r4 - kcar*x(10);
(x(1)*r8)/(J8) - w2*r3;
(x(8)/J10)*r10 - w2*r5;
(x(1)*r9)/(J9 + Js1)*r4 + (diff(x(4)) + diff(x(4),3))/(k0*r4*r4) - w2;
k0*x(6)*r16 - D*r16*(((x(9))*r14)/(Js2 + J1 + (Jcar/3)) - diff(x(6)));
(x(9)*r14)/(Js2 + J1 + (Jcar/3)) - (x(5)*r16)/J16;
x(2)*k0*r8;
x(3)*k0*r10;
kcar*x(10) - x(9)*2*D/(Js1 + J1 + (Jcar/3)) -k0*x(6)*r14;
x(1)/(J9 + Js1) - x(9)/(Js2 + J1 + (Jcar/3))];
[t,xa] = ode45(f,[0 1.5], [0 2 2 2 2 3 1 9 9 1])
Array dimensions are not consistent matlab output error. The commented out code runs for a long time and has not outputted results after running it for 15-20 minutes. The differentials equations are solved using ODE 45. Would really appreciate some help on this. The differential equations come from analyzing a dual clutch manual transmission using the bond graph technique.
Before putting a function like f into ode, try it. It is already broken and you get the same error when calling f(t,x). Further debugging it, evaluate the lines of f one by one:
>> (J4*r9*(diff(x(4),2)))/r4 + (x(4)*khollow*r9)/r4 - kcar*x(10)
ans =
[]
>> (x(1)*r8)/(J8) - w2*r3
ans =
-38000
The first row holds 0 elements, the second 1 element. I did not continue but this is already a problem. Each row has to have the same number of elements, probably 1 in your case.
This is the Fortran code that I modified.
The problem is the code can not open the text file.
I am getting the following error:
The message is: RunCode: Fail to run.. At line 123 of file <unit = 90, file = 'cunnette_xyz.txt'> Fortran runtime error: End of file
The file is below in 4368 rows (decimals are the same for three columns):
602340.440000,129706.190000,28.892939
602340.880000,129706.390000,28.955128
602884.500000,128780.700000,29.876873
602884.380000,128781.190000,29.875114
602884.250000,128781.660000,29.885448
602884.130000,128782.150000,29.895996
602883.940000,128782.630000,29.899380
602883.810000,128783.120000,29.903221
602883.690000,128783.590000,29.907070
602883.560000,128784.080000,29.910931
602883.440000,128784.560000,29.914803
602883.310000,128785.050000,29.918684
602883.190000,128785.520000,29.922575
602883.060000,128786.010000,29.926479
602882.940000,128786.490000,29.930393
602882.810000,128786.980000,29.934317
602882.630000,128787.450000,29.938253
602882.500000,128787.940000,29.942202
.
.
.
the program is:
----------------------------
USE BIEF
USE DECLARATIONS_TELEMAC2D
IMPLICIT NONE
INTEGER LNG,LU, K, ITRAC,I, NSOM,J, NLINE, NDOWN
DOUBLE PRECISION, PARAMETER:: BATHY_RADIER_up= 29.84D0
DOUBLE PRECISION, PARAMETER:: DEPTH_up = 2.15D0
REAL A(5000),B(5000),C(5000)
DOUBLE PRECISION XPOLYD(14), YPOLYD(14), INPOLYD(14)
COMMON/INFO/LNG,LU
DOUBLE PRECISION XPOLY(6), YPOLY(6),COTE_RADIER_up
NSOM = 6
XPOLY(1) = 602883.13
XPOLY(2) = 602886.15
XPOLY(3) = 602887.15
XPOLY(4) = 602905.46
XPOLY(5) = 602902.52
XPOLY(6) = 602884.13
YPOLY(1) = 128779.99
YPOLY(2) = 128780.80
YPOLY(3) = 128777.12
YPOLY(4) = 128741.21
YPOLY(5) = 128739.75
YPOLY(6) = 128775.96
AT = 0.D0
CALL OS( 'X=0 ' , X=U )
CALL OS( 'X=0 ' , X=V )
IF(CDTINI(1:10).EQ.'COTE NULLE'.OR.
* CDTINI(1:14).EQ.'ZERO ELEVATION') THEN
CALL OS( 'X=C ' , H , H , H , 0.D0 )
CALL OS( 'X=X-Y ' , H , ZF , H , 0.D0 )
ELSEIF(CDTINI(1:14).EQ.'COTE CONSTANTE'.OR.
* CDTINI(1:18).EQ.'CONSTANT ELEVATION') THEN
CALL OS( 'X=C ' , H , H , H , COTINI )
CALL OS( 'X=X-Y ' , H , ZF , H , 0.D0 )
ELSEIF(CDTINI(1:13).EQ.'HAUTEUR NULLE'.OR.
* CDTINI(1:10).EQ.'ZERO DEPTH') THEN
CALL OS( 'X=C ' , H , H , H , 0.D0 )
ELSEIF(CDTINI(1:17).EQ.'HAUTEUR CONSTANTE'.OR.
* CDTINI(1:14).EQ.'CONSTANT DEPTH') THEN
CALL OS( 'X=C ' , H , H , H , HAUTIN )
ELSEIF(CDTINI(1:13).EQ.'PARTICULIERES'.OR.
* CDTINI(1:10).EQ.'PARTICULAR'.OR.
* CDTINI(1:07).EQ.'SPECIAL') THEN
COTE_RADIER_up = BATHY_RADIER_up + DEPTH_up
input for coordinates of the downstream
NDOWN = 14
XPOLYD(1) = 602883.13
XPOLYD(2) = 602886.15
XPOLYD(3) = 602864.47
XPOLYD(4) = 602837.90
XPOLYD(5) = 602821.91
XPOLYD(6) = 602649.77
XPOLYD(7) = 602634.35
XPOLYD(8) = 602345.08
XPOLYD(9) = 602326.07
XPOLYD(10) = 602619.31
XPOLYD(11) = 602638.33
XPOLYD(12) = 602811.64
XPOLYD(13) = 602831.52
XPOLYD(14) = 602857.16
YPOLYD(1) = 128779.99
YPOLYD(2) = 128780.80
YPOLYD(3) = 128867.74
YPOLYD(4) = 128936.74
YPOLYD(5) = 128953.95
YPOLYD(6) = 129105.43
YPOLYD(7) = 129143.43
YPOLYD(8) = 129713.38
YPOLYD(9) = 129708.26
YPOLYD(10) = 129136.41
YPOLYD(11) = 129094.72
YPOLYD(12) = 128941.16
YPOLYD(13) = 128931.09
YPOLYD(14) = 128865.81
DO 10 J=1,NPOIN
IF(INPOLY(X(J),Y(J),XPOLY,YPOLY,NSOM)) THEN
write(lu,*) 'upstream is recognized....'
H%R(J)=MAX(0.D0,COTE_RADIER_up-ZF%R(J))
U%R(J)=0.0D0
ELSE
PRINT *, 'opening file'
OPEN(unit =90, FILE = 'cunnette_xyz.txt', FORM ='FORMATTED')
PRINT *, 'read now'
READ (90, *)NLINE
PRINT *, 'DO now'
READ (90, *) A(K),B(K),C(K)
IF(INPOLY(X(J),Y(J),XPOLYD,YPOLYD,NDOWN)) THEN
DO K=1,NLINE
H%R(K)=0.45D0
U%R(K)=0.D0
ENDDO
ELSE
H%R(J)=0.0D0
U%R(J)=0.0D0
ENDIF
ENDIF
write(lu,*) 'downstream ....',K,H%r(K)
10 CONTINUE
ELSE
IF(LNG.EQ.1) THEN
WRITE(LU,*) 'CONDIN : CONDITION INITIALE NON PREVUE : ',CDTINI
ENDIF
IF(LNG.EQ.2) THEN
WRITE(LU,*) 'CONDIN: INITIAL CONDITION UNKNOWN: ',CDTINI
ENDIF
STOP
ENDIF
IF(NTRAC.GT.0) THEN
DO ITRAC=1,NTRAC
CALL OS( 'X=C ' , X=T%ADR(ITRAC)%P , C=TRAC0(ITRAC) )
ENDDO
ENDIF
CALL OS( 'X=C ' , VISC , VISC , VISC , PROPNU )
RETURN
END
This question already has an answer here:
MATLAB : What is the mistake in my Ramachandran plot?
(1 answer)
Closed 8 years ago.
I am trying matlab to plot ramachandran plot, without using built in command. I have succeeded too. Now I wanted to spot the GLYCINEs alone in the scatter array. Any ideas how to do this? (link to 1UBQ.pdb file : http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=pdb&compression=NO&structureId=1UBQ)
% Program to plot Ramanchandran plot of Ubiquitin
close all; clear ; clc; % close all figure windows, clear variables, clear screen
pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb';
p=pdbread(pdb1); % read pdb file corresponding to ubiquitin protein
atom={p.Model.Atom.AtomName};
n_i=find(strcmp(atom,'N')); % Find indices of atoms
ca_i=find(strcmp(atom,'CA'));
c_i=find(strcmp(atom,'C'));
X = [p.Model.Atom.X];
Y = [p.Model.Atom.Y];
Z = [p.Model.Atom.Z];
X_n = X(n_i(2:end)); % X Y Z coordinates of atoms
Y_n = Y(n_i(2:end));
Z_n = Z(n_i(2:end));
X_ca = X(ca_i(2:end));
Y_ca = Y(ca_i(2:end));
Z_ca = Z(ca_i(2:end));
X_c = X(c_i(2:end));
Y_c = Y(c_i(2:end));
Z_c = Z(c_i(2:end));
X_c_ = X(c_i(1:end-1)); % the n-1 th C (C of cabonyl)
Y_c_ = Y(c_i(1:end-1));
Z_c_ = Z(c_i(1:end-1));
V_c_ = [X_c_' Y_c_' Z_c_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_n - V_c_;
V_bc = V_ca - V_n;
V_cd = V_c - V_ca;
phi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
phi=cat(2,phi,-atan2d(y,x));
end
phi=phi(1,2:end);
X_n_ = X(n_i(2:end)); % (n+1) nitrogens
Y_n_ = Y(n_i(2:end));
Z_n_ = Z(n_i(2:end));
X_ca = X(ca_i(1:end-1));
Y_ca = Y(ca_i(1:end-1));
Z_ca = Z(ca_i(1:end-1));
X_n = X(n_i(1:end-1));
Y_n = Y(n_i(1:end-1));
Z_n = Z(n_i(1:end-1));
X_c = X(c_i(1:end-1));
Y_c = Y(c_i(1:end-1));
Z_c = Z(c_i(1:end-1));
V_n_ = [X_n_' Y_n_' Z_n_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_ca - V_n;
V_bc = V_c - V_ca;
V_cd = V_n_ - V_c;
psi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
psi=cat(2,psi,-atan2d(y,x));
end
psi=psi(1,2:end);
scatter(phi,psi)
box on
axis([-180 180 -180 180])
title('Ramachandran Plot for Ubiquitn Protein','FontSize',16)
xlabel('\Phi^o','FontSize',20)
ylabel('\Psi^o','FontSize',20)
grid
The output is :
EDIT : Is my plot correct? Biopython: How to avoid particular amino acid sequences from a protein so as to plot Ramachandran plot? has an answer which has slightly different plot.
The modified code is as below :
% Program to plot Ramanchandran plot of Ubiquitin with no glycines
close all; clear ; clc; % close all figure windows, clear variables, clear screen
pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb';
p=pdbread(pdb1); % read pdb file corresponding to ubiquitin protein
atom={p.Model.Atom.AtomName};
n_i=find(strcmp(atom,'N')); % Find indices of atoms
ca_i=find(strcmp(atom,'CA'));
c_i=find(strcmp(atom,'C'));
X = [p.Model.Atom.X];
Y = [p.Model.Atom.Y];
Z = [p.Model.Atom.Z];
X_n = X(n_i(2:end)); % X Y Z coordinates of atoms
Y_n = Y(n_i(2:end));
Z_n = Z(n_i(2:end));
X_ca = X(ca_i(2:end));
Y_ca = Y(ca_i(2:end));
Z_ca = Z(ca_i(2:end));
X_c = X(c_i(2:end));
Y_c = Y(c_i(2:end));
Z_c = Z(c_i(2:end));
X_c_ = X(c_i(1:end-1)); % the n-1 th C (C of cabonyl)
Y_c_ = Y(c_i(1:end-1));
Z_c_ = Z(c_i(1:end-1));
V_c_ = [X_c_' Y_c_' Z_c_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_n - V_c_;
V_bc = V_ca - V_n;
V_cd = V_c - V_ca;
phi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
phi=cat(2,phi,-atan2d(y,x));
end
phi=phi(1,2:end);
X_n_ = X(n_i(2:end)); % (n+1) nitrogens
Y_n_ = Y(n_i(2:end));
Z_n_ = Z(n_i(2:end));
X_ca = X(ca_i(1:end-1));
Y_ca = Y(ca_i(1:end-1));
Z_ca = Z(ca_i(1:end-1));
X_n = X(n_i(1:end-1));
Y_n = Y(n_i(1:end-1));
Z_n = Z(n_i(1:end-1));
X_c = X(c_i(1:end-1));
Y_c = Y(c_i(1:end-1));
Z_c = Z(c_i(1:end-1));
V_n_ = [X_n_' Y_n_' Z_n_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_ca - V_n;
V_bc = V_c - V_ca;
V_cd = V_n_ - V_c;
psi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
psi=cat(2,psi,-atan2d(y,x));
end
psi=psi(1,2:end);
res=strsplit(p.Sequence.ResidueNames,' ');
angle =[phi;psi];
angle(:,find(strcmp(res,'GLY'))-1)=[];
scatter(angle(1,:),angle(2,:))
box on
axis([-180 180 -180 180])
title('Ramachandran Plot for Ubiquitn Protein','FontSize',16)
xlabel('\Phi^o','FontSize',20)
ylabel('\Psi^o','FontSize',20)
grid
which gives output (with no GLY) as below :
I would change this code block to use logical indexing
res=strsplit(p.Sequence.ResidueNames,' ');
angle =[phi;psi];
angle(:,find(strcmp(res,'GLY'))-1)=[];
Instead:
residues = strsplit(p.Sequency.ResidueNames,' ');
glycine = ismember(residues,'GLY');
angle = [phi;psi];
angleNoGLY= angle(:,~glycine);
Doing it this way, if you wanted to highlight glycine (or any other residue) you can easily call it out:
angleGLY = angle(:,glycine);
plot(angleNoGLY(1,:),angleNoGLY(2,:),'ob')
line(angleGLY(1,:),angleGLY(2,:),'Marker','o','Color','r','LineStyle','none')
I extracted SIFT features from two videos to make a matching.
I need to compare each of the second video features with those stored in an array of features of the first video. I'm having trouble set the code so that when I have the correspondence I can get the frame in which the feature is present. How can I do? Can anyone show me a code example?
This is my code:
obj = VideoReader('video2.avi');
lastFrame = read(obj, inf);
numFrames = obj.NumberOfFrames;
%estrazione frame e sift
for k = 1 : 3 % numFrames / 5
disp(['Processing frame #', num2str(k)]);
this_frame = read(obj, k * 5); % leggi solo un fotogramma ogni 5 per velocizzarle la cosa
this_frame = imresize(this_frame, 0.5); % rimpiccioliamolo per questioni di efficienza!
I = single(rgb2gray(this_frame)) ;
[f,d] = vl_sift(I); % estrazione feature
features{k} = f; % salviamo le feautre e i relativi descrittori in delle celle
descriptors{k} = d;
end
save('feature_input', 'features');
save('descrittori_input', 'descriptors');
%%% un esempio di come ripescare i dati...
pippo = load('feature_input');
newfeat = pippo.features;
pippo = load('descrittori_input');
newdesc = pippo.descriptors;
for k = 1 : 3
disp(['Le feature del fotogramma', num2str(k), ' sono: ']);
f = cell2mat( newfeat(k) );
f(:, 1:10) % ne mostriamo solo un pezzetto... le posizioni delle prime 10 features
end
obj2 = VideoReader('video2u.avi');
lastFrame = read(obj2, inf);
numFrames = obj2.NumberOfFrames;
%estrazione frame e sift video2
for k2 = 1 : 3 % numFrames / 5
disp(['Processing frame #', num2str(k2)]);
this_frame2 = read(obj2, k2 * 5); % leggi solo un fotogramma ogni 5 per velocizzarle la cosa
this_frame2 = imresize(this_frame2, 0.5); % rimpiccioliamolo per questioni di efficienza!
K = single(rgb2gray(this_frame2)) ;
[f2,d2] = vl_sift(K); % estrazione feature
features2{k2} = f2; % salviamo le feautre e i relativi descrittori in delle celle
descriptors2{k2} = d2;
end
save('feature2_input', 'features2');
save('descrittori2_input', 'descriptors2');
%%% un esempio di come ripescare i dati...
pippo2 = load('feature2_input');
newfeat2 = pippo2.features2;
pippo2 = load('descrittori2_input');
newdesc2 = pippo2.descriptors2;
for k2 = 1 : 3
disp(['Le feature del fotogramma', num2str(k2), ' sono: ']);
f2 = cell2mat( newfeat2(k2) );
f2(:, 1:10) % ne mostriamo solo un pezzetto... le posizioni delle prime 10 features
end
[matches, scores] = vl_ubcmatch(d, d2, 1.5) ;
% sift points plot
subplot(1,2,1);
imshow(uint8(I));
hold on;
plot(f(1,matches(1,:)),f(2,matches(1,:)),'b*');
subplot(1,2,2);
imshow(uint8(K));
hold on;
plot(f2(1,matches(2,:)),f2(2,matches(2,:)),'r*');
figure;
%-------------
% RANSAC
X1 = f(1:2,matches(1,:)) ; X1(3,:) = 1 ;
X2 = f2(1:2,matches(2,:)) ; X2(3,:) = 1 ;
numMatches = size(matches,2) ;
for t = 1:100
% estimate homograpyh
subset = vl_colsubset(1:numMatches, 4) ;
A = [] ;
for i = subset
A = cat(1, A, kron(X1(:,i)', vl_hat(X2(:,i)))) ;
end
[U,S,V] = svd(A) ;
H{t} = reshape(V(:,9),3,3) ;
% score homography
X2_ = H{t} * X1 ;
du = X2_(1,:)./X2_(3,:) - X2(1,:)./X2(3,:) ;
dv = X2_(2,:)./X2_(3,:) - X2(2,:)./X2(3,:) ;
ok{t} = (du.*du + dv.*dv) < 6*6 ;
score(t) = sum(ok{t}) ;
end
[score, best] = max(score) ;
H = H{best};
ok = ok{best};
% sift feature matching
dh1 = max(size(K,1)-size(I,1),0) ;
dh2 = max(size(I,1)-size(K,1),0) ;
subplot(2,1,1) ;
imagesc([padarray(I,dh1,'post') padarray(K,dh2,'post')]) ;
colormap (gray);
o = size(I,2) ;
line([f(1,matches(1,:));f2(1,matches(2,:))+o], ...
[f(2,matches(1,:));f2(2,matches(2,:))]) ;
axis image off ;
subplot(2,1,2) ;
imagesc([padarray(I,dh1,'post') padarray(K,dh2,'post')]) ;
colormap (gray);
o = size(I,2) ;
line([f(1,matches(1,ok));f2(1,matches(2,ok))+o], ...
[f(2,matches(1,ok));f2(2,matches(2,ok))]) ;
title(sprintf('%d (%.2f%%) inliner matches out of %d', ...
sum(ok), ...
100*sum(ok)/numMatches, ...
numMatches)) ;
axis image off ;
drawnow ;
end
You are matching using [matches, scores] = vl_ubcmatch(d, d2, 1.5) ;. d contains only the descriptors of the latest frame. You should do something like:
for nFrames=1:3
[matches{nFrames}, scores{nFrames}] = vl_ubcmatch(descriptors{nFrames}, descriptors2{nFrames}, 1.5);
end
From here, you should be able to get the matches between the frames.