Calculate mean between 3D matrix - matlab

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

Related

Reduce three dimensional array to a vector of significant numbers

Hi I have a three dimensional array in Matlab, something like <10 x 10 x 100> and I would like to reduce this array to a vector of significant numbers. For example I would like to take each matrix(picture) split it in half by columns, compute sum(left)-sum(right) and return this <1 x 100> vector back. Unfortunately I cannot figure out or find out how to do that. Is it possible? And how could I achieve it?
Thanks a lot for any help.
Here's a one-liner, given a matrix A:
result = -squeeze(diff(sum(reshape(A, [50 2 100]), 1), 1, 2)).';
How it works:
First, reshape the data into a 50-by-2-by-100 matrix where values from the left half of each matrix are in column 1 and values from the right half of each matrix are in column 2. Then apply sum down each column to get a 1-by-2-by-100 matrix. You can then take the difference between the columns with diff, although this subtracts the left column from the right, so you have to add a minus to negate the result. The resulting 1-by-1-by-100 matrix can be collapsed to a 100-by-1 column vector with squeeze, and this can be transposed into a row vector. Alternatively, you can use another reshape instead of the squeeze and transpose:
result = -reshape(diff(sum(reshape(A, [50 2 100]), 1), 1, 2), [1 100]);

Calculate Euclidean distance for every row with every other row in a NxM matrix?

I have a matrix that I generate from a CSV file as follows:
X = xlsread('filename.csv');
I am looping through the matrix based on the number of records and I need to find the Euclidean distance for each of the rows of this matrix :
for i = 1:length(X)
j = X(:, [2:5])
end
The resulting matrix is of 150 X 4. What would be the best way to calculate the Euclidean distance of each row (with 4 columns as the data points) with every row and getting an average of the same?
In order to find the Euclidean distance between any pair of rows, you could use the function pdist.
X = randn(6, 4);
D = pdist(X,'euclidean');
res=mean(D);
The average is stored in res.

Vector and matrix comparison in MATLAB

I have vector with 5 numbers in it, and a matrix of size 6000x20, so every row has 20 numbers. I want to count how many of the 6000 rows contain all values from the vector.
As the vector is a part of a matrix which has 80'000'000 rows, each containing unique combinations, I want a fast solution (which doesn't take more than 2 days).
Thanks
With the sizes you have, a bsxfun-based approach that builds an intermediate 6000x20x5 3D-array is affordable:
v = randi(9,1,5); %// example vector
M = randi(9,6000,20); %// example matrix
t = bsxfun(#eq, M, reshape(v,1,1,[]));
result = sum(all(any(t,2),3));

Checking equality of row elements in Matlab?

I have a matrix A in Matlab of dimension mxn. I want to construct a vector B of dimension mx1 such that B(i)=1 if all elements of A(i,:) are equal and 0 otherwise. Any suggestion? E.g.
A=[1 2 3; 9 9 9; 2 2 2; 1 1 4]
B=[0;1;1;0]
One way with diff -
B = all(diff(A,[],2)==0,2)
Or With bsxfun -
B = all(bsxfun(#eq,A,A(:,1)),2)
Here's another example that's a bit more obfuscated, but also does the job:
B = sum(histc(A,unique(A),2) ~= 0, 2) == 1;
So how does this work? histc counts the frequency or occurrence of numbers in a dataset. What's cool about histc is that we can compute the frequency along a dimension independently, so what we can do is calculate the frequency of values along each row of the matrix A separately. The first parameter to histc is the matrix you want to compute the frequency of values of. The second parameter denotes the edges, or which values you are looking at in your matrix that you want to compute the frequencies of. We can specify all possible values by using unique on the entire matrix. The next parameter is the dimension we want to operate on, and I want to work along all of the columns so 2 is specified.
The result from histc will give us a M x N matrix where M is the total number of rows in our matrix A and N is the total number of unique values in A. Next, if a row contains all equal values, there should be only one value in this row where all of the values were binned at this location where the rest of the values are zero. As such, we determine which values in this matrix are non-zero and store this into a result matrix, then sum along the columns of the result matrix and see if each row has a sum of 1. If it does, then this row of A qualifies as having all of the same values.
Certainly not as efficient as Divakar's diff and bsxfun method, but an alternative since he took the two methods I would have used :P
Some more alternatives:
B = var(A,[],2)==0;
B = max(A,[],2)==min(A,[],2)

3D matrix averaring in 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.