Minimum mean square in three dimension - matlab

I have two matrix, X and Y, both of them are of dimensions (4,24,16) correspondent to indices (i,j,z), I need to get the minimal mean square between then and it's index with reference to j. I means I will need to compare every vector and get the minimal difference in the vector j. I mean I should get the index between 1 and 24. here are example matrix,
clear all; clc; clear;
X = randn(4,24,16);
Y = randn(4,24,16);
but then I stuck how to do the minimum mean square with respect to second dimension j !
please any help ?

Execute mean on third and first dimension, thus resulting in second dimension that you can find the minimum value on:
MSE2=mean(mean( (X-Y).^2 ,3),1)
[mmse, argmmse]=min(MSE2);

Related

How can I find the minima of a 3D matrix in a given dimension?

So I want to take the minimum of a 3D matrix, but I want to be able to chose in which dimension I want to take the minimum. For example, if I have a random 2x3x4 matrix. I want the minimum in the z direction (i.e. 3rd dimension).
M = rand(2, 3, 4)
MinMatrix = function(M, direction)
With direction I mean per row, per column, or per z direction. Now say I want the minimum in the z direction, so the function should give me the MinMatrix which is 2x3 in dimension and gives me the minima of the 4 numbers in the z direction:
Min1 Min2 Min3
Min4 Min5 Min6
This is the code I tried just to get the minimum, without the direction part yet:
function [MinMatrix] = functionname(M)
MinMatrix = min(M(:, :, 1:4));
end
I found this question, along with a similar question on The MathWorks website, but that involves reshaping the original matrix which I would like to avoid and you have to change the code to change the dimension. It's unclear from those questions how I can solve this.
You need to specify the dimension argument for the min function:
MinMatrix = min(M, [], 3); % Minimum along the 3rd dimension
Note that, in this case, you have to pass an empty matrix for the second argument (an easy thing to forget when working with min and max).

Matlab ./ sign; basic matlab

I am a newbie to Matlab and am trying to figure out code. One thing that keeps returning and confusing me is the ./ sign. I tried googling and stackoverflowing it, but I can not find any documentation on it. Just googling ./ in combination with Matlab does not get me there.
To my understanding the code in the bottom does: Take the first record of the contry.farm.potatoes and divide this by all country.farm.tractors values summed. The result however is always a 0.x numbers. So from 0 to 1. Does that mean that the ./ sign makes sure it is a percentage?
country.farm.potatoes(1,:)./sum(country.farm.tractors,1)
General explanation
the following code does the following:
(1) takes the first row of country.farm.potatoes
(2) generate a vector such that the i'th coordinate is the sum of the i'th column of country.farm.tractors
(3) divides each coordinate of (1) by the corresponding coordinate in (2).
(:,1) syntax
Given a matrix M of size m,n, the syntax '(:,1)' extracts the first row of the matrix.
It generates a row vector of size 1xn.
M(1,:)
sum(M,1) syntax
Given a matrix M of size m,n, the syntax sum(M,1) generates a row vecotr of size 1xn,
s.t each coordinate j is the sum of the j'th column of the matrix.
sum(M,1)
A ./ B syntax
Given two matrices or vectors A,B (of same size), the syntax 'C=A./B' yields a per coordinate division result.
This means that C(i,j)=A(i,j)/B(i,j)
C = A./B
for example:
[9,6,2] ./ [3,3,2]
ans = 3 2 1

Matlab : Help in finding minimum distance

I am trying to find the point that is at a minimum distance from the candidate set. Z is a matrix where the rows are the dimension and columns indicate points. Computing the inter-point distances, and then recording the point with minimum distance and its distance as well. Below is the code snippet. The code works fine for a small dimension and small set of points. But, it takes a long time for large data set (N = 1 million data points and dimension is also high). Is there an efficient way?
I suggest that you use pdist to do the heavy lifting for you. This function will compute the pairwise distance between every two points in your array. The resulting vector has to be put into matrix form using squareform in order to find the minimal value for each pair:
N = 100;
Z = rand(2,N); % each column is a 2-dimensional point
% pdist assumes that the second index corresponds to dimensions
% so we need to transpose inside pdist()
distmatrix = squareform(pdist(Z.','euclidean')); % output is [N, N] in size
% set diagonal values to infinity to avoid getting 0 self-distance as minimum
distmatrix = distmatrix + diag(inf(1,size(distmatrix,1)));
mindists = min(distmatrix,[],2); % find the minimum for each row
sum_dist = sum(mindists); % sum of minimal distance between each pair of points
This computes every pair twice, but I think this is true for your original implementation.
The idea is that pdist computes the pairwise distance between the columns of its input. So we put the transpose of Z into pdist. Since the full output is always a square matrix with zero diagonal, pdist is implemented such that it only returns the values above the diagonal, in a vector. So a call to squareform is needed to get the proper distance matrix. Then, the row-wise minimum of this matrix have to be found, but first we have to exclude the zero in the diagonals. I was lazy so I put inf into the diagonals, to make sure that the minimum is elsewhere. In the end we just have to sum up the minimal distances.

From a vector to a shorter vector of averages in Matlab

Let I have a vector x in Matlab of size 1000. I want to produce a vector y of length 10 where each component contains the average of 100 records from the vector x.
My attempt is quite simple
for i=1:10
y(i) = mean(x((1:100)+(i-1)*100));
end
I wonder if there is a build in command or more elegant solution for this.
You can use the reshape-function to generate a 2d-array and then calculate the mean over the correct dimension.
This works as replacement for your loop:
y = mean(reshape(x,[100,10]),1);
Note: It does not matter if x is a column-vector or a row-vector.

Remove duplicates in correlations in matlab

Please see the following issue:
P=rand(4,4);
for i=1:size(P,2)
for j=1:size(P,2)
[r,p]=corr(P(:,i),P(:,j))
end
end
Clearly, the loop will cause the number of correlations to be doubled (i.e., corr(P(:,1),P(:,4)) and corr(P(:,4),P(:,1)). Does anyone have a suggestion on how to avoid this? Perhaps not using a loop?
Thanks!
I have four suggestions for you, depending on what exactly you are doing to compute your matrices. I'm assuming the example you gave is a simplified version of what needs to be done.
First Method - Adjusting the inner loop index
One thing you can do is change your j loop index so that it only goes from 1 up to i. This way, you get a lower triangular matrix and just concentrate on the values within the lower triangular half of your matrix. The upper half would essentially be all set to zero. In other words:
for i = 1 : size(P,2)
for j = 1 : i
%// Your code here
end
end
Second Method - Leave it unchanged, but then use unique
You can go ahead and use the same matrix like you did before with the full two for loops, but you can then filter the duplicates by using unique. In other words, you can do this:
[Y,indices] = unique(P);
Y will give you a list of unique values within the matrix P and indices will give you the locations of where these occurred within P. Note that these are column major indices, and so if you wanted to find the row and column locations of where these locations occur, you can do:
[rows,cols] = ind2sub(size(P), indices);
Third Method - Use pdist and squareform
Since you're looking for a solution that requires no loops, take a look at the pdist function. Given a M x N matrix, pdist will find distances between each pair of rows in a matrix. squareform will then transform these distances into a matrix like what you have seen above. In other words, do this:
dists = pdist(P.', 'correlation');
distMatrix = squareform(dists);
Fourth Method - Use the corr method straight out of the box
You can just use corr in the following way:
[rho, pvals] = corr(P);
corr in this case will produce a m x m matrix that contains the correlation coefficient between each pair of columns an n x m matrix stored in P.
Hopefully one of these will work!
this works ?
for i=1:size(P,2)
for j=1:i
Since you are just correlating each column with the other, then why not just use (straight from the documentation)
[Rho,Pval] = corr(P);
I don't have the Statistics Toolbox, but according to http://www.mathworks.com/help/stats/corr.html,
corr(X) returns a p-by-p matrix containing the pairwise linear correlation coefficient between each pair of columns in the n-by-p matrix X.