Is there an efficient way (i.e., without copying data) in openCV to create vector along the third dimension of a 3D Mat, like in MATLAB:
X=rand(3,4,5);
V=squeeze(X(2,2,:));% vector along the third dimension of elements in second row and col.
Thanks
Related
How can one explain the principle of fft in a multidimensional array?
Y = fft(X)
If X is a multidimensional array, then fft(X) treats the values along the first array dimension whose size does not equal 1 as vectors and returns the Fourier transform of each vector.
I don't understand it very well.
X=[1 2 ; 3 4];
X(:,:,2)=[5 6 ; 7 8]
Let's take the 2D case for simplicity.
Given a 2D matrix of data, X, one can treat each row vector individually and perform a one-dimensional discrete Fourier transform. This will decompose each row into one dimensional frequency components. No attention is given to whether the rows of the matrix are correlated, and so every row of the output matrix will have some non-zero components.
Alternatively, using fft2, one can decompose X into its constituent two-dimensional frequency components. I find this easiest to think about in analogy with the DCT, where you can visualize the 2D basis easily
With the 2D FFT, correlations between rows would affect the output of the FFT. It's possible a single 2D frequency component (which is more like a surface than a wave) could contain all the energy of the 2D FFT, and so the output matrix could be much sparser than when treating the rows individually.
This analogy can be continued in to n-dimensions, with higher dimensional frequency components potentially capturing the higher dimensional structure in your data.
Let's assume I have a 8x6x2 matrix signals. I would like to sum along the columns excluding the first column. When using the following code, MATLAB concatenates the 3D matrix to a big 2D (8x11) matrix, which is different from the result I am looking for.
sum(signals(:, 2:end), 2)
I am actually looking for a 8x1x2 3D vector comprising the sums of column 2 to six from each third dimension.
Since your matrix is a 3D matrix, you need to include a colon as the third subscript in your indexing. If you only specify two subscripts, then MATLAB will collapse all trailing dimensions into the last dimension you've specified.
sum(signals(:, 2:end, :), 2)
I attempted to use the solution from this post: Multiply a 3D matrix with a 2D matrix, to vectorize the multiplication of a one dimensional with a three dimensional, but to no avail. Any help would be greatly appreciated!
for s = 1: s_length
for a = 1: a_length
for g = g_length
two_dim(s,a) = two_dim(s,a) + one_dim(g) * three_dim(s,a,g);
end
end
end
I think this does what you want.
two_dim = reshape(three_dim, [], size(three_dim,3))*one_dim(:);
two_dim = reshape(two_dim, size(three_dim,1), size(three_dim,2));
This works as follows:
First line reshapes your 3D array into a matrix, collapsing the first two dimensions into one. That way the operation you want is standard multiplication of the resulting matrix times a column vector.
Second line reshapes the result into matrix shape.
mtimes does not work with inputs with dimension larger than 2, so you have to find a way to do the multiplication by yourself. The solution of Luis Mendo is a nice solution; here is another one using bsxfun:
two_dim = two_dim + squeeze(sum(bsxfun(#times, three_dim, reshape(one_dim,[1 1 g_length])),3));
Here is how it works:
reshape makes the vector one_dim looking like a 3D array. This must be done because the multiplication between the vector and the 3D array is performed along the 3rd dimension, so Matlab need a hint on sizes.
bsxfun perfoms the element-wise multiplcation, so the result must be sumed up along the 3rd dimension (and squeezed to be compliant with a 2D matrix format)
I want to create a 3D matrix of (MxNx1) dimension. where third dimension is equal to 1.
I am trying something like zeros(M,N,1) but it is returning a matrix of (MxN) dimension instead of (MxNx1) dimension. Is there any way of creating a 3D matrix where third dimension equal to 1.
How do you process a MxNxD image? I could only image that the useage of the third dimension is:
[M,N,D] = size(your3DMatrix);
Then you use D for further coding. Another possible useage is
permute(your3DMatrix,[n1 n2 n3])
Anyway, these both work fine for matrix whose third dimension is 1.
If you detail your code dealing with the MxNx1 matrix, it would be easier to help you.
I am wondering if there is any build in function or an easy way to plot a histogram of elements of a 2d array.
For example, if A=rand(100,1), then A is an 1D array, and hist(A) can do the histogram.
However, what if A=rand(100,100), and I would like to make a histogram on elements of A, just like treating each element in A as an element on a 1D array. Is there a easy way to do so?
You just have to reshape A into a vector, then you can use hist as usual:
hist(A(:))
This command will do what you want:
hist(reshape(A, prod(size(A)), 1))
What it does is create a vector out of the matrix A by reshaping it into a matrix with one column and a number of rows equal to the number of elements of A:
prod(size(A)) = number_of_columns(A) * number_of_rows(A)
Or the short way:
hist(A(:))
This takes every element of A in sequence and thus also generates a vector.