Dimensions of matrices being concatenated are not consistent using vertcat - matlab

I get this error messge everytime I run my code. I have searched through other questions that are the same and tried the solutions but they have not worked. The error message is below followed by the code.
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in project2 (line 147)
e = [a0;c0;d0];
N = length(z1);
if length(w)~=N, error('z and sw must be same length');
end
M = N-1;
a0 = zeros(2*M-2,(3*M));
b0 = zeros(2*M-2,1);
for i = 1:M-1
co = i;
ro = 2*(i-1)+1;
a0(ro,co) = w(i+1)^2;
a0(ro+1,co+1) = w(i+1)^2;
a0(ro,co+n) = w(i+1);
a0(ro+1,co+n+1) = w(i+1);
a0(ro,co+2*n) = 1;
a0(ro+1,co+2*n+1) = 1;
b0(ro) = z1(i+1);
b0(ro+1) = z1(i+1);
end
c0 = zeros(M-1,(3*M));
for i = 1:M-1
c0(i,i) = 2*w(i+1);
c0(i,i+1) = -2*w(i+1);
c0(i,i+n) = 1;
c0(i,i+n+1) = -1;
end
d0 = zeros(2,(3*M));
d0(1,1) = w(1)^2;
d0(1,M+1) = w(1);
d0(1,2*M+1) = 1;
d0(2,M) = w(end)^2;
d0(2,2*M) = w(end);
d0(2,end) = 1;
e = [a0;c0;d0];

Related

Order of complexity of a matrix (matlab)

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

Error: In assignment A(I) = B, the number of elements in B and I must be the same

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.

Why do I get such a bad loss in my implementation of k-Nearest Neighbor?

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!

writing function with two outputs

I have written the following code. I would like the function to return the matrices D & S but at the moment it simply returns the matrix 'ans' which is equal to S. Any suggestions would be appreciated. Thanks.
function [D,S] = sdQRS(QRS_cell)
%scales & dilates QRS complexes
m = length(QRS_cell{1}(:,1));
n = length(QRS_cell);
d1 = linspace(0.5,1.0,10);
d2 = linspace(1.0,2.0,21);
d = vertcat(d1',(d2(2:21))');
s1 = linspace(0.6,1.0,13);
s2 = linspace(1.0,1.5,18);
s = vertcat(s1',(s2(2:18))');
DIL = cell(n,1);
SCAL = cell(n,1);
for i = 1:n
DIL{i} = zeros(m,length(d));
SCAL{i} = zeros(m,length(d));
for j = 1:length(d)
DIL{i}(:,j) = interp1(QRS_cell{i}(:,1),QRS_cell{i}(:,2),QRS_cell{i}(:,1)/d(j),'linear','extrap');
SCAL{i}(:,j) = s(j)*QRS_cell{i}(:,2);
end
end
D = zeros(n);
S = zeros(n);
for k = 1:n
for l = 1:n
e = [];
t = [];
for a = 1:length(d)
e(a) = euc_dilQ(QRS_cell{k},QRS_cell{l},d(a));
t(a) = euc_scalQ(QRS_cell{k},QRS_cell{l},s(a));
end
[M,E] = min(e);
[M,T] = min(t);
D(k,l) = d(E);
S(k,l) = s(T);
end
end
You can specify a Matlab function to return any number of output values. In your case the function signature would look something like
function [D,S] = sdQRS(QRS_cell) {
d1 = linspace(0.5,1.0,10);
...
}
Now you can call that function by entering
[D,S] = sdQRS(QRS_cell);

Subscripted assignment dimension mismatch in matlab

I executed this code using Feature Matrix 517*11 and Label Matrix 517*1. But once the dimensions of matrices change the code cant be run. How can I fix this?
The error is:
Subscripted assignment dimension mismatch.
in this line :
edges(k,j) = quantlevels(a);
Here is my code:
function [features,weights] = MI(features,labels,Q)
if nargin <3
Q = 12;
end
edges = zeros(size(features,2),Q+1);
for k = 1:size(features,2)
minval = min(features(:,k));
maxval = max(features(:,k));
if minval==maxval
continue;
end
quantlevels = minval:(maxval-minval)/500:maxval;
N = histc(features(:,k),quantlevels);
totsamples = size(features,1);
N_cum = cumsum(N);
edges(k,1) = -Inf;
stepsize = totsamples/Q;
for j = 1:Q-1
a = find(N_cum > j.*stepsize,1);
edges(k,j) = quantlevels(a);
end
edges(k,j+2) = Inf;
end
S = zeros(size(features));
for k = 1:size(S,2)
S(:,k) = quantize(features(:,k),edges(k,:))+1;
end
I = zeros(size(features,2),1);
for k = 1:size(features,2)
I(k) = computeMI(S(:,k),labels,0);
end
[weights,features] = sort(I,'descend');
%% EOF
function [I,M,SP] = computeMI(seq1,seq2,lag)
if nargin <3
lag = 0;
end
if(length(seq1) ~= length(seq2))
error('Input sequences are of different length');
end
lambda1 = max(seq1);
symbol_count1 = zeros(lambda1,1);
for k = 1:lambda1
symbol_count1(k) = sum(seq1 == k);
end
symbol_prob1 = symbol_count1./sum(symbol_count1)+0.000001;
lambda2 = max(seq2);
symbol_count2 = zeros(lambda2,1);
for k = 1:lambda2
symbol_count2(k) = sum(seq2 == k);
end
symbol_prob2 = symbol_count2./sum(symbol_count2)+0.000001;
M = zeros(lambda1,lambda2);
if(lag > 0)
for k = 1:length(seq1)-lag
loc1 = seq1(k);
loc2 = seq2(k+lag);
M(loc1,loc2) = M(loc1,loc2)+1;
end
else
for k = abs(lag)+1:length(seq1)
loc1 = seq1(k);
loc2 = seq2(k+lag);
M(loc1,loc2) = M(loc1,loc2)+1;
end
end
SP = symbol_prob1*symbol_prob2';
M = M./sum(M(:))+0.000001;
I = sum(sum(M.*log2(M./SP)));
function y = quantize(x, q)
x = x(:);
nx = length(x);
nq = length(q);
y = sum(repmat(x,1,nq)>repmat(q,nx,1),2);
I've run the function several times without getting any error.
I've used as input for "seq1" and "seq2" arrays such as 1:10 and 11:20
Possible error might rise in the loops
for k = 1:lambda1
symbol_count1(k) = sum(seq1 == k);
end
if "seq1" and "seq2" are defined as matrices since sum will return an array while
symbol_count1(k)
is expected to be single value.
Another possible error might rise if seq1 and seq2 are not of type integer since they are used as indexes in
M(loc1,loc2) = M(loc1,loc2)+1;
Hope this helps.