Variance of elements in matrices (element-by-element) in MATLAB - matlab

I am trying to compute the variance of elements which are organised in matrices (in MATLAB). As an example, let's be A and B two matrices 2x2.
My goal is to find the matrix V (2x2 as well), being the variance of each element of A and each element of B, that is:
Can somebody help me on this?

This is a very simple use case of the var function:
A = [1 2;
3 4];
B = [5 6;
7 8];
V0 = var(cat(3,A,B),0,3);
V1 = var(cat(3,A,B),1,3);
This results in:
V0 =
8 8
8 8
V1 =
4 4
4 4
What happens is that you concatenate your matrices along some unused dimension and then compute the variance along that dimensions.
NOTE: The example of 2 matrices is not very meaningful, but I'm assuming your actual dataset is larger, in which case you could use this method.

Related

Using splines() forms problem if x coordinates go from increasing to decreasing

I have two columns with data, x and y. Now I want to connect these data points in the order they appear in the columns. Say I have x=[1 2 3 4 3 2] and y=[3 4 2 1 3 3]. Now if I use spline to create a smooth curve, it 'sorts' the columns in an increasing order. I would like it to just take the data points, thus firstly x(1),y(1) and connect these to x(2), y(2) and so on.
Is this possible?
spline generates a function from the reals to the reals. This means a more general curve cannot be expressed as y = f(x) but we need to parametrize it as (x(t), y(t)):
x=[1 2 3 4 3 2];
y=[3 4 2 1 3 3];
plot(x,y,'o-');
% cannot be represented as function y=f(x)
% because x=2 and 3 have two different y values
% -> parametrize x and y:
t = 1:numel(x);
tt = linspace(min(t), max(t), 1000);;
tx = spline(t,x,tt);
ty = spline(t,y,tt);
hold on
plot(tx,ty,'-');

Angles between n vectors - Matlab

Consider a set of points (just an example)
x = [0 1 2 5 4 8 5 6];
y = [5 8 4 2 5 6 4 5];
and another reference point:
xc=1;
yc=1;
In which I use to represent these points as vectors:
vec=[x-xc y-yc];
I wish to obtain a matrix with all the angles between all vectors which is obtained by the calculation (for single vectors)
angle = acosd(dot(v,u)/norm(u)*norm(v));
How can I obtain this calculation in a few lines without going vector by vector in a loop? In my calculation the number of points is very very large.
I think you mean vec = [x-xc; y-yc];. To calucate the dotproduct between all rows, you can use
vec.'*vec
The norm (Euclidean) of each vector can be determined as
no = sqrt(sum(vec.*vec,1))
The product of the different norms can be calculated the same as for vec:
no.'*no
The angles can thus be found as
no = sqrt(sum(vec.*vec,1));
angles = acosd(vec.'*vec./(no.'*no));

How to do data dimensionailty reduction?

I have a set of 25 images of label 'Infected' and 25 images of label 'Normal'.
I am trying to extract the dual-tree complex wavelet transform based coefficients as features for each of the images.
My code to obtain coefficients using DT-CWT ia as follows:
I = imread('infected_img1.jpg'); %read image
I = rgb2gray(I); %rgb ro gray-scale
L = 6; %no. of levels for wavelet decomposition
I = reshape(I',1,size(I,1)*size(I,2)); %change into a vector
I = [I,zeros(1,2^L - rem(length(I),2^L))]; %pad zeros to make dim(I) a multiple of 2^L
I = double(I);
dt = dddtree('cplxdt',I,L,'dtf3'); %perform DT-CWT
dt_Coeffs = (dt.cfs{L}(:,:,1) + 1i*dt.cfs{L}(:,:,2)); %extract coefficents at Level 6
Now, since I have 24 more images to extract coefficients from, I do this block for each of the images. My ultimate aim is to append all coefficient vectors generated in each iteration to form a matrix. But each image tends to give a different sized coefficient vector.
I want to know about some dimension reduction method that can reduce each vector to a uniform size and at the same time preserve its information.
Can anyone suggest methods with a good amount of clarity?
As I mentioned in my comment,
You can't shrink something (i.e. remove information) and still preserve all of the information.
Instead you can pad all of the vectors to the length of the largest vector and then concatenate them to create a matrix. You could program your own method but in the spirit of not reinventing the wheel I've previously used padcat(). It is very easy to use and pads with NaN but you can easily change this to 0.
Here's an example usage which also contains a handy conversion from NaN to 0.
>> a = [1 2 3 4];
>> b = [1 2 3];
>> c = padcat(a, b);
c =
1 2 3 4
1 2 3 NaN
>> c(isnan(c)) = 0
c =
1 2 3 4
1 2 3 0

Matlab: Solve Exponential Equation with two unknown parameters

I have a Matrix called A. For example the following:
A = [1 2 3; 3 4 1; 2 4 4]
Now I have the following equation:
A(x,y) = (j^x)*(i^y)
j and i are normal values (dimension 1x1), not indices of a matrix. ^
Lets make an example:
A(1,1) = 1 (First value of the Matrix)
1 = (j^1)*(i^1)
And a second one:
A(1,2) = 3
3 = (j^1)*(i^2)
Is there a possibility to receive one solution for the two parameters using Matlab?
Here is some code that can find the best solution to your problem, if there is one. In this case, there is no reasonable solution, but defining A by M([4 2]) (for example) does work reasonably well.
A = [1 2 3; 3 4 1; 2 4 4] %// the A matrix
[C,R]=meshgrid(1:3) %// create matrices of row/column indices
M=#(xy) xy(2).^C.*xy(1).^R %// calculates matrix of elements j^x*i^y
d=#(xy) A-M(xy) %// calculates difference between A and the calculated i^x*y^j matrix
r=fsolve(#(xy) norm(d(xy)),[1 1]) %// use fsolve to attempt to find a solution
d(r) %// show resulting difference between target matrix and solution matrix
norm(d(r)) %// norm of that matrix
M(r) %// show the solution matrix

MATLAB: Matrix containing values of another matrix at specific indices

I need help solving an indexing problem. The assigned problem states: Two matrices (x and y) give the coordinates to form matrix B from matrix A. Produce the matrix B which contains the values of A at the given coordinates in x and y.
For instance:
x = [1 1 1; 2 2 1]
y = [1 2 1; 3 2 4]
%This would read as (1,1),(1,2),(1,1),(2,3),(2,2),(1,4)
% Given matrix:
A = [6 7 8 9; 10 11 12 13];
%This would give us this answer for B (using the coordinate scheme above):
B=[6 7 6; 12 11 9];
I'm guessing I need to use the find function in conjunction with a sub2ind function, but I'm not 100% sure how to translate that into working code. The only thing I can think of would be to do something like this:
B=((x(1),(y(1)), (x(2),y(2)).......
But that would only work for the defined matrix above, not a randomly generated matrix. I tried looking for a similar problem on the site, but I couldn't find one. Your help would be really appreciated!
You can't do it for randomly generated matrices, because you have to ensure that matrix A has lines and columns as required from the values of x and y.
In this case, you can write:
for i=1:length(x(:))
B(i)=A(x(i),y(i));
end
B=reshape(B,size(x));