When i'm running this code in matlab, it is printing the g4 value into the array columns were g3 and its calculated value is suppose to be, as well as its own column. I was just wondering how to stop g4 being placed into g3's column and instead, print g3 and its value in the two arrays.
Cheers
v_meas = 0;
g1 = 1.09;
g2 = 0.9;
g3 = 0.93;
g4 = 0.85;
radius = 3.75;
K = 0.006;
m = g1;
g = g3;
for ii = 1 : 1 : 2
v_meas = m*((radius^2)*pi)*K;
ArrayOfDarceys(1,ii) = v_meas;
ArrayOfGradients(1,ii) = m;
v_meas = 0;
m = g2;
for jj = 3 : 1 : 4
v_meas = g*((radius^2)*pi)*K;
ArrayOfDarceys(1,jj) = v_meas;
ArrayOfGradients(1,jj) = g;
v_meas = 0;
g = g4;
end
end
ArrayOfDarceys
ArrayOfGradients
I suspect you didn't intend to nest your for loops. Try this:
for ii = 1 : 1: 2
v_meas = m*((radius^2)*pi)*K;
ArrayOfDarceys(1,ii) = v_meas;
ArrayOfGradients(1,ii) = m;
v_meas = 0;
m = g2;
end
for jj = 3 : 1: 4
v_meas = g*((radius^2)*pi)*K;
ArrayOfDarceys(1,jj) = v_meas;
ArrayOfGradients(1,jj) = g;
v_meas = 0;
g = g4;
end
If I understand what you are trying to do you could significantly simplify your code though. There is, in fact, no need for any for loops:
ArrayofGradients = [1.09,0.9,0.93,0.85]
ArrayofDarceys = ArrayofGradients*((radius^2)*pi)*K
Related
I need some help answering this question : How do I find the complexity of the matrix B and f knowing that I have this code :
d = 123456;
randn('state',d); rand('state',d);
n = 1000; A = diag(1+rand(1,n));
k = 5500; r = randperm(n*n,k);
A(r) = A(r) + rand(1,k);
L = tril(A); U = A-L;
x = rand(n,1);
b = A*x;
B = -L\U; f = L\b;
xit = zeros(n,1);
nit = 100;
res = zeros(nit,1);
incr = zeros(nit,1);
err = zeros(nit,1);
for it = 1:nit,
xit0 = xit;
xit = B*xit+f;
res(it) = norm(b-A*xit);
incr(it) = norm(xit-xit0);
err(it) = norm(xit-x);
end
Thanks in advance
I have a while loop and I've been unable to determine how to store the values successfully. Any help would be greatly appreciated.
n = 0; a = 21; b = 0; c = 0; y = 37.6991; T = 18.5; z = 0.1591; d = 0; f = 15.3049; q = 2.2391e4; e = 5; x = 60;
while n < x
a = a + b
c = y*(T-a)/z;
d = f*c;
b = d/q;
n = n + e;
end
The value I'm trying to store is a, I can tell the values are correct inside the loop but just can't seem to store the values correctly.
Another approach would be to recognise that it is a relatively simple recurrence relation:
n = 0; a = 21; y = 37.6991; T = 18.5; z = 0.1591; f = 15.3049; q = 2.2391e4; e = 5; x = 60;
while n < x
a(end+1) = a(end) + f * y * (T - a(end)) / (q * z);
n = n + e;
end
This calculation can also be vectorised, but if you want exactly the same output you need to be a little bit careful:
n = 5:5:55; y = 37.6991; T = 18.5; z = 0.1591; f = 15.3049; q = 2.2391e4; a0 = 21;
alpha = f * y * T / (q * z);
beta = (1 - f * y / (q * z)).^(0:length(n))
a = a0 * beta + alpha * [0 cumsum(beta(1:end-1))];
The code seems to lose clarity (to me) when vectorised, so I would probably prefer the loop in this scenario.
Try this :
counter = 1;
n = 0; a = 21; b = 0; c = 0; y = 37.6991; T = 18.5; z = 0.1591; d = 0; f = 15.3049; q = 2.2391e4; e = 5; x = 60;
var = zeros(1,12);
while n < x
a = a + b;
c = y*(T-a)/z;
d = f*c;
b = d/q;
n = n + e;
var(counter) = a;
counter = counter+1;
end
I added a variable called var which is a vector that stores the values of a. In order t osave runtime i initialized it to the expected size of the variable var = zeros(1,12); (This is not strictly required but is recommended.
I'm stuck on K2 as it brought up this error:
In an assignment A(I) = B, the number of elements in B and
I must be the same.
I ran the debugger and I found out that J4 alone was a vector while other variables were all scalar.
How can I resolve this error to have the plot?
Here is the code that I ran.
h1 = 1*10^-6;
h2 = (10:10:1500)*10^-6;
a = 62.5*10^-6;
b = a+h1;
c = b+h2;
alpha_1 = 0.55*10^-6;
alpha_2 = 17.2*10^-6;
alpha_3 = 14.2*10^-6;
zeta = 6.3*10^-6;
P11 = 0.121;
P12 = 0.27;
neff = 1.456;
U1 = 0.17;
U2 = 0.32;
U3 = 0.31;
E1 = 0.74*10^11;
E2 = 1.08*10^11;
E3 = 1.96*10^11;
n = 1;
while(n<=150)
J1(n) = E2*(b^2-a^2)*(1-U1)-E1*a^2*(1-U2)-E1*b^2*(1+U2);
J2(n) = 2*E1*b^2;
J3(n) = E1*E2*(b^2-a^2)*(alpha_2 - alpha_1);
J4(n) = 2*E3*(c(n)^2-b^2)*a^2;
J5(n) = E2*(b^2-a^2)*(1-U3)*b^2+E2*(b^2-a^2)*(1+U3)*c(n)^2-E3*(c(n)^2-b^2)*(1+U2)*a^2-E3*(c(n)^2-b^2)*(1-U2)*b^2;
J6(n) = E2*E3*(c(n)^2 - b^2)*(b^2-a^2)*(alpha_2-alpha_3);
K1(n) = ((alpha_3-alpha_1)*E3*(c(n)^2-b^2)+(alpha_2-alpha_1)*E2*(b^2-a^2))/(E1*a^2+E2*(b^2-a^2)+E3*(c(n)^2-b^2));
K2(n) = (J2*J6-J3*J5)/(J2*J4-J1*J5);
Sr(n) = (neff^2/2)*(P11+P12)*(((1-U1)*K2/E1)-U1*K1);
Sz(n) = (1+P12)*(K1-(2*U2*K2/E1));
St(n) = alpha_1+zeta;
Km(n) = St+Sz+Sr;
n=n+1;
end
plot(h2,Km)
To recap what was already said in one answer, here's how I would modify the code:
h1 = 1e-6;
h2 = (10:10:1500)*1e-6;
a = 62.5*1e-6;
b = a+h1;
c = b+h2;
alpha_1 = 0.55*1e-6;
alpha_2 = 17.2*1e-6;
alpha_3 = 14.2*1e-6;
zeta = 6.3*1e-6;
P11 = 0.121;
P12 = 0.27;
neff = 1.456;
U1 = 0.17;
U2 = 0.32;
U3 = 0.31;
E1 = 0.74*1e11;
E2 = 1.08*1e11;
E3 = 1.96*1e11;
% pre-allocate variables
J1 = zeros(size(h2));
J2 = zeros(size(h2));
J3 = zeros(size(h2));
J4 = zeros(size(h2));
J5 = zeros(size(h2));
J6 = zeros(size(h2));
K1 = zeros(size(h2));
K2 = zeros(size(h2));
Sr = zeros(size(h2));
Sz = zeros(size(h2));
for n=1:length(h2)
J1(n) = E2*(b^2-a^2)*(1-U1)-E1*a^2*(1-U2)-E1*b^2*(1+U2);
J2(n) = 2*E1*b^2;
J3(n) = E1*E2*(b^2-a^2)*(alpha_2 - alpha_1);
J4(n) = 2*E3*(c(n)^2-b^2)*a^2;
J5(n) = E2*(b^2-a^2)*(1-U3)*b^2+E2*(b^2-a^2)*(1+U3)*c(n)^2-E3*(c(n)^2-b^2)*(1+U2)*a^2-E3*(c(n)^2-b^2)*(1-U2)*b^2;
J6(n) = E2*E3*(c(n)^2 - b^2)*(b^2-a^2)*(alpha_2-alpha_3);
K1(n) = ((alpha_3-alpha_1)*E3*(c(n)^2-b^2)+(alpha_2-alpha_1)*E2*(b^2-a^2))/(E1*a^2+E2*(b^2-a^2)+E3*(c(n)^2-b^2));
K2(n) = (J2(n)*J6(n)-J3(n)*J5(n))/(J2(n)*J4(n)-J1(n)*J5(n));
Sr(n) = (neff^2/2)*(P11+P12)*(((1-U1)*K2(n)/E1)-U1*K1(n));
Sz(n) = (1+P12)*(K1(n)-(2*U2*K2(n)/E1));
end
St = alpha_1+zeta;
Km = Sz+Sr+St;
plot(h2,Km)
Notes:
I have used a for loop to ensure the vector lengths are consistent with h2
I have pre-allocated the variables for speed
I have added various (n) to K1, K2, J1, J2, etc... in the equations to have only scalar operations
I have moved stuff out of the for loop that didn't need to be there
This gives the following plot (in Octave)
I ran your code and I found out that the dimensions of the vectors used to compute K2 are not compatible, e.g. each of J2 and J6 is a 1-row by 2-columns vector and you cannot multiply those. This also applies for the other multiplications. Depending on what you want to compute, you should transpose either one of them in each multiplication.
FYI: J.' is the transposed version of J in MATLAB.
I'm trying to implement k-NN in matlab. I have a matrix of 214 x's that have 9 columns of attributes with the 10th column being the label. I want to measure loss with a 0-1 function on 10 cross-validation tests. I have the following code:
function q3(file)
data = knnfile(file);
loss(data(:,1:9),'KFold',data(:,10))
losses = zeros(25,3);
new_data = data;
new_data(:,10) = [];
sdd = std(new_data);
meand = mean(new_data);
for s = 1:214
for q = 1:9
new_data(s,q) = (new_data(s,q) - meand(q)) / sdd(q);
end
end
new_data = [new_data data(:,10)];
for k = 1:25
loss1 = 0;
loss2 = 0;
for j = 0:9
index = floor(214/10)*j+1;
curd1 = data([1:index-1,index+21:end],:);
curd2 = new_data([1:index-1,index+21:end],:);
for l = 0:20
c1 = knn(curd1,k,data(index+l,:));
c2 = knn(curd2,k,new_data(index+l,:));
loss1 = loss1 + (c1 ~= data(index+l,10));
loss2 = loss2 + (c2 ~= new_data(index+l,10));
end
end
losses(k,1) = k;
losses(k,2) = 100*loss1/210;
losses(k,3) = 100*loss2/210;
end
function cluster = knn(Data,k,x)
distances = zeros(193,2);
for i = 1:size(Data,1)
row = Data(i,:);
d = norm(row(1:size(row,2)-1) - x(1:size(x,2)-1));
distances(i,:) = [d row(10)];
end
distances = sortrows(distances,1);
cluster = mode(distances(1:k,2));
I'm getting 40%+ loss with almost no correlation to k and I'm sure that something here is wrong but I'm not quite sure.
Any help would be appreciated!
I'm doing quite simple SVM classification at the moment. I use a precomputed kernel in LibSVM with RBF and DTW.
When I compute the similarity (kernel-) matrix, everything seems to work very fine ... until I permute my data, before I compute the kernel matrix.
An SVM is of course invariant to permutations of input-data. In the below Matlab-code, the line marked with '<- !!!!!!!!!!' decides about the classification accuracy (not permuted: 100% -- permuted: 0% to 100%, dependant on the seed of rng). But why does permuting the file-string-array (named fileList) make any difference? What am I doing wrong? Have I misunderstood the concept of 'permutation invariance' or is it a problem with my Matlab-code?
My csv-files are formatted as: LABEL, val1, val2, ..., valN and all the csv-files are stored in the folder dirName. So the string array contains the entries '10_0.csv 10_1.csv .... 11_7.csv, 11_8.csv' (not permuted) or some other order when permuted.
I also tried to permute the vector of sample serial numbers, too, but that makes no difference.
function [SimilarityMatrixTrain, SimilarityMatrixTest, trainLabels, testLabels, PermSimilarityMatrixTrain, PermSimilarityMatrixTest, permTrainLabels, permTestLabels] = computeDistanceMatrix(dirName, verificationClass, trainFrac)
fileList = getAllFiles(dirName);
fileList = fileList(1:36);
trainLabels = [];
testLabels = [];
trainFiles = {};
testFiles = {};
permTrainLabels = [];
permTestLabels = [];
permTrainFiles = {};
permTestFiles = {};
n = 0;
sigma = 0.01;
trainFiles = fileList(1:2:end);
testFiles = fileList(2:2:end);
rng(3);
permTrain = randperm(length(trainFiles))
%rng(3); <- !!!!!!!!!!!
permTest = randperm(length(testFiles));
permTrainFiles = trainFiles(permTrain)
permTestFiles = testFiles(permTest);
noTrain = size(trainFiles);
noTest = size(testFiles);
SimilarityMatrixTrain = eye(noTrain);
PermSimilarityMatrixTrain = (noTrain);
SimilarityMatrixTest = eye(noTest);
PermSimilarityMatrixTest = eye(noTest);
% UNPERM
%Train
for i = 1 : noTrain
x = csvread(trainFiles{i});
label = x(1);
trainLabels = [trainLabels, label];
for j = 1 : noTrain
y = csvread(trainFiles{j});
dtwDistance = dtwWrapper(x(2:end), y(2:end));
rbfValue = exp((dtwDistance.^2)./(-2*sigma));
SimilarityMatrixTrain(i, j) = rbfValue;
n=n+1
end
end
SimilarityMatrixTrain = [(1:size(SimilarityMatrixTrain, 1))', SimilarityMatrixTrain];
%Test
for i = 1 : noTest
x = csvread(testFiles{i});
label = x(1);
testLabels = [testLabels, label];
for j = 1 : noTest
y = csvread(testFiles{j});
dtwDistance = dtwWrapper(x(2:end), y(2:end));
rbfValue = exp((dtwDistance.^2)./(-2*sigma));
SimilarityMatrixTest(i, j) = rbfValue;
n=n+1
end
end
SimilarityMatrixTest = [(1:size(SimilarityMatrixTest, 1))', SimilarityMatrixTest];
% PERM
%Train
for i = 1 : noTrain
x = csvread(permTrainFiles{i});
label = x(1);
permTrainLabels = [permTrainLabels, label];
for j = 1 : noTrain
y = csvread(permTrainFiles{j});
dtwDistance = dtwWrapper(x(2:end), y(2:end));
rbfValue = exp((dtwDistance.^2)./(-2*sigma));
PermSimilarityMatrixTrain(i, j) = rbfValue;
n=n+1
end
end
PermSimilarityMatrixTrain = [(1:size(PermSimilarityMatrixTrain, 1))', PermSimilarityMatrixTrain];
%Test
for i = 1 : noTest
x = csvread(permTestFiles{i});
label = x(1);
permTestLabels = [permTestLabels, label];
for j = 1 : noTest
y = csvread(permTestFiles{j});
dtwDistance = dtwWrapper(x(2:end), y(2:end));
rbfValue = exp((dtwDistance.^2)./(-2*sigma));
PermSimilarityMatrixTest(i, j) = rbfValue;
n=n+1
end
end
PermSimilarityMatrixTest = [(1:size(PermSimilarityMatrixTest, 1))', PermSimilarityMatrixTest];
mdlU = svmtrain(trainLabels', SimilarityMatrixTrain, '-t 4 -c 0.5');
mdlP = svmtrain(permTrainLabels', PermSimilarityMatrixTrain, '-t 4 -c 0.5');
[pclassU, xU, yU] = svmpredict(testLabels', SimilarityMatrixTest, mdlU);
[pclassP, xP, yP] = svmpredict(permTestLabels', PermSimilarityMatrixTest, mdlP);
xU
xP
end
I'd be very thankful for any answer!
Regards
Benjamin
after cleaning up the code and letting a colleague of mine have a look on it, we/he finally found the bug. Of course, I have to compute the testing matrix from the training and testing samples (to let the SVM predict the testing data by using the sum over the product of alpha-values of the training vectors (they are zero for non support vectors)). Hope this clarifies the problem for any of you. To make it more clear, see my revised code below. But, as for example in using precomputed kernels with libsvm, there one with sharp eyes can also see the computation of the testing matrix with train and test vectors, too. Feel free to put comments or/and answers to this post if you have any further remarks/questions/tips!
function [tacc, testacc, mdl, SimilarityMatrixTrain, SimilarityMatrixTest, trainLabels, testLabels] = computeSimilarityMatrix(dirName)
fileList = getAllFiles(dirName);
fileList = fileList(1:72);
trainLabels = [];
testLabels = [];
trainFiles = {};
testFiles = {};
n = 0;
sigma = 0.01;
trainFiles = fileList(1:2:end);
testFiles = fileList(2:5:end);
noTrain = size(trainFiles);
noTest = size(testFiles);
permTrain = randperm(noTrain(1));
permTest = randperm(noTest(1));
trainFiles = trainFiles(permTrain);
testFiles = testFiles(permTest);
%Train
for i = 1 : noTrain(1)
x = csvread(trainFiles{i});
label = x(1);
trainlabel = label;
trainLabels = [trainLabels, label];
for j = 1 : noTrain(1)
y = csvread(trainFiles{j});
dtwDistance = dtwWrapper(x(2:end), y(2:end));
rbfValue = exp((dtwDistance.^2)./(-2*sigma.^2));
SimilarityMatrixTrain(i, j) = rbfValue;
end
end
SimilarityMatrixTrain = [(1:size(SimilarityMatrixTrain, 1))', SimilarityMatrixTrain];
%Test
for i = 1 : noTest(1)
x = csvread(testFiles{i});
label = x(1);
testlabel = label;
testLabels = [testLabels, label];
for j = 1 : noTrain(1)
y = csvread(trainFiles{j});
dtwDistance = dtwWrapper(x(2:end), y(2:end));
rbfValue = exp((dtwDistance.^2)./(-2*sigma.^2));
SimilarityMatrixTest(i, j) = rbfValue;
end
end
SimilarityMatrixTest = [(1:size(SimilarityMatrixTest, 1))', SimilarityMatrixTest];
mdlU = svmtrain(trainLabels', SimilarityMatrixTrain, '-t 4 -c 1000 -q');
fprintf('TEST: '); [pclassU, xU, yU] = svmpredict(testLabels', SimilarityMatrixTest, mdlU);
fprintf('TRAIN: ');[pclassT, xT, yT] = svmpredict(trainLabels', SimilarityMatrixTrain, mdlU);
tacc = xT(1);
testacc = xU(1);
mdl = mdlU;
end
Regards
Benjamin