3D matrix averaring in matlab - matlab

i have R 3d matrix,n varies from 1:100.
I have generated 20 such R matrix.
Now i have to average each R for this 20 experiment.
so that I'll get n,100 avg matrix.
How to average this 20, n Matrix?
I want to add(avg) all 20 times generated R for each n .I must have avg 100 R matrix .

Assuming you actually have a 3D matrix R, it is very easy to average:
R = rand(3,4,5); %Suppose this is your matrix
Now you just need to pick the dimension you want to average in:
mean(R,1) %First dimension
mean(R,2) %Second dimension
mean(R,3) %Third dimension
If you are not sure which one you need, just check the size of all three.

Related

Calculate mean between 3D matrix

I have a matrix (100x50, it has random numbers) stored as x(:,:,1) and x(:,:,2). I want to calculate the average corresponding to the row and column of these matrixes but no luck so far. I tried to use the mean function but it gives me only one value. Any tips for an algorithm?
You can get for each matrix like the following:
mean(x(:,:,1),1) //avg in columns of x(:,:,1)
mean(x(:,:,1),2) //avg in row of x(:,:,1)
Also you can get the mean of x in different dimension using the following code:
mean(x,3); // size 100x50, avg of element of the two matrices
mean(x,2); // size 100 x 1 x 2, avg of rows of the two matrices
mean(x,1); // size 1 x 50 x 2, avg of columns of the two matrices

Reducing dimensionality of features with PCA in MATLAB

I'm totally confused regarding PCA. I have a 4D image of size 90x60x12x350. That means that each voxel is a vector of size 350 (time series).
Now I divide the 3D image (90x60x12) into cubes. So let's say a cube contains n voxels, so I have n vectors of size 350. I want to reduce this n vectors to only one vector and then calculate the correlations between all vectors of all cubes.
So for a cube I can construct the matrix M where I just put each voxel after each other, i.e. M = [v1 v2 v3 ... vn] and each v is of size 350.
Now I can apply PCA in Matlab by using [coeff, score, latent, ~, explained] = pca(M); and taking the first component. And now my confusion begins.
Should I transpose the matrix M, i.e. PCA(M')?
Should I take the first column of coeff or of score?
This third question is now a bit unrelated. Let's assume we have a
matrix A = rand(30,100) where the rows are the datapoints and the
columns are the features. Now I want to reduce the dimensionality of
the feature vectors but keeping all data points.
How can I do this with PCA?
When I do [coeff, score, latent, ~, explained] = pca(M); then
coeff is of dimension 100x29 and score is of size 30x29. I'm
totally confused.
Yes, according to the pca help, "Rows of X correspond to observations and columns to variables."
score just tells you the representation of M in the principal component space. You want the first column of coeff.
numberOfDimensions = 5;
coeff = pca(A);
reducedDimension = coeff(:,1:numberOfDimensions);
reducedData = A * reducedDimension;
I disagree with the answer above.
[coeff,score]=pca(A)
where A has rows as observations and column as features.
If A has 3 featuers and >3 observations (Let's say 100) and you want the "feature" of 2 dimensions, say matrix B (the size of B is 100X2). What you should do is:
B = score(:,1:2);

mat2cell to divide 3D image into blocks

I have a 4D image of size 60 x 80 x 12 x 350, i.e. it is a 3D image where each voxel has a time series (of 350).
Now I want to use mat2cell to divide the 3D image into cubes of dimension k*k*k. Each voxel in the cube is a vector of size 350 (the time series).
I think I could do it with mat2cell but I don't know how exactly. Each cell should contain in the end a 3D block of the image where each voxel of the block is a vector of size 350.
Assuming your 4D matrix is called M. You need to have vectors whose elements sum to size(M, i) where i = 1:4. Assuming k has some value, I tried both 4 (because it's a common factor of the sizes you specified) and 3 (because it's not).
k = 3;
MPrime = mat2cell(M, ...
[k*ones(1, floor(size(M,1)/k)), mod(size(M,1), k)], ...
[k*ones(1, floor(size(M,2)/k)), mod(size(M,2), k)], ...
[k*ones(1, floor(size(M,3)/k)), mod(size(M,3), k)], ...
ones(1, size(M,4)));

How to realize this in Matlab?

There is a matrix X, the size of which is 400-by-1000. I want to collect L = 10 samples, each of size M = 500, drawn without replacement from a uniform distribution over X. How to realize it by using Matlab? Anyone can give me a help?
The easiest thing would be to use randsample from the statistics toolbox. This allows you to take a random sample from a population / data without replacement. However, randsample takes in a vector, but you want to sample from a matrix. Judging from the context, each element in this matrix is equally likely to be chosen, so if we simply convert the matrix into a vector and we sample from this vector, you should achieve the same result.
If you want 10 samples of length 500, you can either call randsample once and get 5000 samples from the matrix, and reshape this into a 10 x 500 matrix where each row represents one 500 element sample, or loop this call 10 times and concatenate the results at each call.
So you can do this:
Y = randsample(X(:), 5000);
Y = reshape(Y, 10, 500);
The above code generates a 5000 x 1 vector and we reshape it into a 10 x 500 matrix.
Or you can do this:
Y = [];
for idx = 1 : 10
Y = [Y randsample(X(:), 500)];
end
Y = Y.';
randsample generates a 500 x 1 vector at each call, so we will stack these column vectors horizontally, then transpose the result so we get a 10 x 500 matrix.
However, if you don't have randsample, you can use randperm to generate a random permutation of numbers from 1 to 400 x 1000, and select 5000 elements from this permutation. You can then index into X to obtain your final matrix. Something like:
idx = randperm(400*1000, 5000);
Y = reshape(X(idx), 10, 500);

Calculating the degree matrix having the sparse representation of the adjacency matrix

I am trying to calculate the laplacian matrix of a graph. I ve calculated the sparse representation of the adjacency matrix which is stored in a text file with dimension Nx3. N the size of nodes (ith-node jth node weight). I open in Matlab this file with adj = spconvert(adj);. The next step is to calculate the degree matrix of this sparse matrix in order to perform the operation L = D - adj. How is it possible to calculate the degree matrix having as an input the sparse adjacency matrix of the graph? In order to calculate the degree matrix I calculate the degree for every node:
for i=1:n % size of the node
degree(i) = length(find(adj(:,1) == i & adj(:,3) == 1));
end
However, how can I perform the subtraction of D and A?
Use the spdiags function to convert the degree vector to a sparse diagonal matrix. Then subtract the adjacency matrix from diagonal matrix to get the Laplacian. Example using your code:
adj = spconvert(adj);
for i=1:size(adj, 1)
degree(i) = CalcDegree(adj, i)
end
D = spdiags(degree, 0, size(adj, 1), size(adj, 2));
L = D - adj;
By the way, your code for calculating the node degree may be incorrect.