I am trying to make a Z-test in Octave, but I have a weird error.
This is my code:
A =[ 7 7 4 5 9 9
4 12 8 1 8 7
3 13 2 1 17 7
12 5 6 2 1 13
14 10 2 4 9 11
3 5 12 6 10 7 ];
x = A(:)';
n = length(x);
fprintf(' Volume %d \n',n)
med = mean(x);
fprintf(' Sample mean %f\n',med)
sigma = 5;
m0 = 9;
type = -1; %left tailed test
alpha1 = 0.05; %significance level
alpha2 = 0.01;
fprintf('\n')
fprintf('SIGNIFICANCE LEVEL %f:\n',alpha1)
[h1, p1, ci1, zstat1] = ztest(x, m0, sigma, alpha1, type);
And the error is:
error: Invalid Name argument.
error: called from
ztest at line 70 column 9
Pb1a at line 26 column 23
I'd like to know why this happens. Hope it is not because I am using Octave instead of Matlab. Actually that is...it works in MATLAB Online.
The error is pretty explanatory: the syntax you are using (five numeric arguments) is not supported by Octave. In fact, in Matlab that syntax is not documented either, and seems to be supported only for backward compatibility, as can be seen in ztest source code:
if nargin>=4 <p>
if isnumeric(varargin{1})
% Old syntax
% ZTEST(X,M,SIGMA,ALPHA,TAIL,DIM)
[...]
The solution is to use the syntax officially supported by both Matlab and Octave, with arguments beyond the third specified as parameter, value pairs:
[h1, p1, ci1, zstat1] = ztest(x, m0, sigma, 'alpha', alpha1, 'tail', 'left');
Related
I am asking an upgraded and more focused version of my query as I think I remained unable to explain properly in my last query.
I want to down sample my signal based on new time array.
I have time array and sample array.
t = [0 2 3 7 8 9 10 11 12 17 18 19 20];
A = [0 0 1 2 3 5.2 0 -1.4 0 2 2.7 2 2.3];
New time array is:
Tx = 1:4:25;
I am using interpolation proposed by Andrei Davydov on stackoverflow but I think I am at fault some point.
Can someone point me out where I am at fault? Thanks a lot in advance.
If the value of A is same at Tx as w.r.t (t)then use that value and if no value is found interpolate value and assign that value to new time .
Example Code:
t = [0 2 3 7 8 9 10 11 12 17 18 19 20 ];
A = [0 0 1 2 3 5.2 0 -1.4 0 2 2.7 2 2.3 ];
plot(t,A)
Tx = 1:4:25;
B = interp1(t,A,Tx); %re-make example data to have decimal points on the x-axis
y = resample(B, 1, 2);
T = 0.05;
Ty = T / (1 / 2);
ty = (0:length(y)-1)*Ty;
figure
plot(Tx,B,'b')
hold on
plot(ty,y,'r')
plot(t,A,'g')
hold off
Link to my previous question is attached here.
Resampling of time signal in MATLAB
Note :
This is now exactly what i want so this is more clear and mature.
I am using MATLAB version 2012b so please provide me solution w.r.t that as there are few Matlab build in command which don't work in 2012b.
The main Your problem - you try to extrapolate, as original time ends at 20, but Yours ends at 25. Try this code:
clc
t = [0 2 3 7 8 9 10 11 12 17 18 19 20 ];
A = [0 0 1 2 3 5.2 0 -1.4 0 2 2.7 2 2.3 ];
Tx = 1:4:25; % Are you sure you want to extrapolate?
% max(Tx1)>max(t)
% This variant is WITHOUT extrapolation
B = interp1(t,A,Tx);
% This variant is WITH extrapolation and WHOLE time-series interpolated
% cubically
extrapBcub=interp1(t,A,Tx,'pcchip');
% If you want to have linear (default) interpolation, but cubic
% extrapolation then
extrapBlin=[B(~isnan(B)), extrapBcub(isnan(B))];
It gives the following figure:
f=figure('Position',[50 50 1500 800])
h1=subplot(1,2,1);
hold all
h(1)=plot(t,A,'-ok','LineWidth',3)
h(2)=plot(Tx,B,'-ob','LineWidth',9)
h(3)=plot(Tx,extrapBcub,':or','LineWidth',7)
h(4)=plot(Tx,extrapBlin,'-og','LineWidth',3)
xlabel('time')
ylabel('signal')
set(gca,'Box','off','Color','none','FontSize',14,'LineWidth',2)
legend({'Original data','No extrapolation','Cubic all',...
'Linear interpolation+cubic extrapolation'},'Location','SouthOutside',...
'FontSize',22)
legend boxoff
h2=subplot(1,2,2);
hold all
h3 = copyobj(h(end:-1:1), h2) % copy plots just for scaling
ylim([-2 6])
xlabel('time')
ylabel('signal')
set(gca,'Box','off','Color','none','FontSize',14,'LineWidth',2)
How can I fix this error?
mvnpdf.m
% y = mvnpdf(x,mu,Sigma)
% Compute multivariate normal pdf for x given mean mu and covariance matrix
% sigma. The dimension of x is d x p, mu is 1 x p and sigma is p x p.
function pdf = mvnpdf(x,mu,sigma)
[d,p] = size(x);
% mu can be a scalar, a 1xp vector or a nxp matrix
if nargin == 1, mu = 0; end
if all(size(mu) == [1,p]), mu = repmat(mu,[d,1]); end
if nargin < 3
pdf = (2*pi)^(-p/2) * exp(-sumsq(x-mu,2)/2);
else
r = chol(sigma);
pdf = (2*pi)^(-p/2) * exp(-sumsq((x-mu)/r,2)/2) / prod(diag(r));
end
pdfdep.m
function pdfmx = pdfdep(train, test)
% computes probability density for all classes
% assuming feature independence
% train - train set; the first column contains label
% used to compute mean and variation for all classes
% test - test set (without labels)
% pdfmx - matrix of probability density for all classes
% class with label idx is stored in pdfmx(:,idx)
classnb = rows(unique(train(:,1)));
pdfmx = ones(rows(test), classnb);
for cl=1:classnb
clidx = train(:,1) == cl;
mu = mean(train(clidx,2:end));
sigma = cov(train(clidx,2:end));
pdfmx(:,cl) = mvnpdf(test, mu, sigma);
end
mat.txt
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 1
3 4 5 6 7 8 1 2
4 5 6 7 8 1 2 3
1 8 7 6 5 4 3 2
2 7 6 5 4 3 2 9
3 6 5 4 3 2 9 8
4 5 4 3 2 9 8 7
1 8 7 6 5 4 3 2
3 6 5 4 3 2 9 8
Error message:
>> mat2 = mat;
>> pdfdep(mat, mat2)
error: chol: input matrix must be positive definite
error: called from
mvnpdf at line 13 column 7
pdfdep at line 20 column 15
>>
The error is quite self explanatory
input matrix must be positive definite
Means that your matrix (sigma) is not positive definite, thus you cannot run cholesky decomposition on it. There are many ways used to estimate covariance in a nice manner, simply computing the empirical estimate (what you do by calling cov) does not work when your data is degenerated (it lies in low dimensional manifold). One of the simplest solutions is to use "pulled estimator" of form:
cov(X) + eps * I
instead of
cov(X)
thus just change
sigma = cov(train(clidx,2:end));
to incorporate this additional + eps * I (where I is identity matrix of appropriate dimension).
I am trying to write my own im2col algorithm for input dimensions > 2D.
Currently I am looking at the Matlab im2col implementation. However, I cannot find any documentation regarding what is going on for any input of more than 2 dimensions.
I do get an output if I feed in a 3D tensor into the function. However I don't really understand how you get from 2D to ND. The fact that this isn't mentioned in the documentation suggests that its something straightforward, still, I don't get it.
Heck, I dont even understand why the size of the output matrix is the size it is.
Let me just start by saying that im2col is only intended for 2D matrices. The fact that it sometimes worked (and by that I mean returned a result without throwing an error) is just a happy coincidence.
Now I took a peek at edit im2col.m, and without studying the code too much, the first line of each of the distinct and sliding methods should give you an intuition of what's happening:
...
if strcmp(kind, 'distinct')
[m,n] = size(a);
...
elseif strcmp(kind,'sliding')
[ma,na] = size(a);
...
end
...
First recall that [s1,s2] = size(arr) where arr is a 3d array will collapse the size of 2nd and 3rd dimension into one size. Here's the relevant doc size:
[d1,d2,d3,...,dn] = size(X) returns the sizes of the dimensions of the array X, provided the number of output arguments n equals ndims(X). If n < ndims(X), di equals the size of the ith dimension of X for 0<i<n, but dn equals the product of the sizes of the remaining dimensions of X, that is, dimensions n through ndims(X).
So basically for an array of size M-by-N-by-P, the function instead thinks it's a matrix of size M-by-(N*P). Now MATLAB has some quirky indexing rules that lets you do things like:
>> x = reshape(1:4*3*2,4,3,2)
x(:,:,1) =
1 5 9
2 6 10
3 7 11
4 8 12
x(:,:,2) =
13 17 21
14 18 22
15 19 23
16 20 24
>> x(:,:)
ans =
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 23
4 8 12 16 20 24
which is what I think ended up happening here. Here is an example to confirm the behavior of im2col on an RGB image:
% normal case (grayscale image)
>> M = magic(5);
>> B1 = im2col(M, [3 3], 'sliding');
% (RGB image)
>> MM = cat(3, M, M+50, M+100);
>> B2 = im2col(MM, [3 3], 'sliding');
>> B3 = im2col(reshape(MM, [5 5*3]), [3 3], 'sliding');
>> assert(isequal(B2,B3))
Note that B2 and B3 are equal, so basically think of the result of im2col on an array arr = cat(3,R,G,B) to be the same as that of arr = cat(2,R,G,B) (concatenated horizontally).
Interestingly, you won't get so lucky with "distinct" blocks method:
>> B1 = im2col(M, [3 3], 'distinct') % works
% ..snip..
>> B2 = im2col(MM, [3 3], 'distinct') % errors
Subscripted assignment dimension mismatch.
Error in im2col (line 59)
aa(1:m,1:n) = a;
Now that we understand what was happening, let's think how to do this properly for 3D arrays.
In my opinion to implement im2col for color images, I would just run it on each color channel separately (each being a 2d matrix), and concatenate the result along the third dimension. So something like this wrapper function:
function B = im2col_rgb(img, sz, varargin)
B = cell(1,size(img,3));
for i=1:size(img,3)
B{i} = im2col(img(:,:,i), sz, varargin{:});
end
B = cat(3, B{:});
end
Say we have the following matrix
1 3 6
5 4 7
5 3 9
What I'm trying to do is for each row, I assign it the maximum value of the column. So, for instance, I'm expecting the following output:
x(1) = 6
x(2) = 7
x(3) = 9
I tried doing that using by writing the code below, but didn't get the expected putput:
x=[1 3 6;5 4 7; 5 3 9]
[rows, columns] = size(x);
for i=1:columns
for j=1:rows
[maximum, position] = max(x(j,:));
disp('MAXIMUM')
x(j)=maximum
end
end
What should I do to get the expected output?
You can use the built-in max function with a dimension specifier: max(x,[],dim).
In your case, assuming your matrix is called A:
>> x=max(A,[],2)
ans =
6
7
9
If I understood correctly your question, you can just use the max function. It naturally operates on columns, therfore, some transposition is necessary.
x=[1 3 6;5 4 7; 5 3 9]
y = max(x')'
y =
6
7
9
You can even reassing the values on the fly
x = max(x')'.
Iam facing problem in understanding and converting a matlab code into opencv. I want to know is there any equivalent function of sub2ind as in matlab in opencv. Or how to implement in opencv this particular function.
link for sub2ind function is
http://www.mathworks.in/help/techdoc/ref/sub2ind.html
A quick example to illustrate. Consider:
>> v = (1:4*3)
v =
1 2 3 4 5 6 7 8 9 10 11 12
>> M = reshape(v,[4 3])
M =
1 5 9
2 6 10
3 7 11
4 8 12
Now all the following are equivalent:
sz = size(M);
i = 3; j = 2;
M(i,j)
v( sub2ind(sz,i,j) )
v( sz(1)*(j-1)+i )
Just keep in mind that MATLAB uses a column-major order, while C is row-major order