How can I subset a matrix from a larger one in matlab? - matrix

how to subset a matrix of different dimension from another one in matlab?Thanks for any help.

Related

k nearest neighbour search for cells in place of matrices?

I want to find the euclidean distance between some datasets. For each class I have about 50 learning or training samples and 5 test samples. The problem is that this data for each sample is stored in cells and is in the m * n format rather than the preferred 1 * n format.
I see that matlab has a built in function for k nearest neighbours using euclidean distance; knnsearch. But the problem is that it expects the samples to be in the form of rows and columns. It gives an error when data in cells are provided.
Is there anyway to do this classification in the format my data is in? Or is it preferred that I convert them to 1-D format (maybe by using some sort of dimensionality reduction?)
Suppose there are 5 classes/models that are each represented by cells of size 32 * N, below is a screenshot from Matlab. Each cell represents the features of a lot of training images, so cell 1 is a representation of person 1 and so on.
Now similarly I have test images that need to be identified. The features of each test image are also in cells. So I have 10 unknown images with their features extracted and stored in cells. Each cell is a representation of a single unknown image. Below again is a screenshot from Matlab showing their dimensions
As can be seen they are all of varying dimensions. I know this is not the ideal format of KNN as here each image is represented by a multidimension matrix rather than a single row of variables. How do I perform euclidean distance on such data?
Thank you

Given a list of vectors in R3 how many unique planes are generated?

While investigating packings of spheres I ran into this problem where I have a list of vectors and I want to know how many planes they generate. I'm generating these lists of vectors that point from the center of a sphere to a contact point on the outside surface of the sphere and I want to know how many of these vectors are coplanar. For example I want an algorithm that will do the following..
Given the vectors {1,2,3}, {2,4,6}, and {0,6,9} it should report that there is only one unique plane generated by either one of the first two and the third.
All of my attempts haven't gotten anywhere because every time I count how many planes are generated I drastically over count. I feel like this should be an easy thing to do and I'm also curios if there is any linear algebra that can somehow come to the rescue. If I can determine how many planes are generated and what those planes are I think it will be easy to determine how many vectors lie in each plane which is the last part of this problem. If anyone can think of a more general approach to any dimension two or greater that would actually be ideal but for now this is all I'm concerned with.
You can use Gaussian elimination to determine the dimension of the span of several vectors. (the space that is created by all linear combinations of these - also called rank of the matrix)
Create a matrix from your vectors by writing the vectors in the rows of your matrix. Then use Gaussian elimination and count the number of Rows that still have non-zero entries. This is the dimension of the spanned vectorspace.
Since you use vectors from R^3 this will never be greater than 3. However a plane is a 2-dimensional vectorspace, so you need to find all combinations of vectors that result in a 2-dimensional span, which can be easily found by iterating over your vectors once you have implemented gaussian elimination.
Edit:
An example since it seems that this can still lead to confusion:
you have a set of 3 vectors: (1,0,0); (0,1,0); (0,0,1)
You can create 3 different planes from these (by combining any two of these vectors you get a different plane.) To formally check if that statement is true you need to do the following for every pair v1,v2 of vectors:
check whether v1,v2 are linear independant - if they aren't, they don't create a plane, so you go ahead and pick the next two vectors. (in this example they always are linear independant)
Check for each other vector in your list, if it (v3) does not lie within the created plane (it is not coplanar to v1,v2). To do this use gaussian elimination on the matrix (v1,v2,v3) and confirm that the rank of the matrix is 3.
If the matrix in step 2 has a rank of 2, this means that the vectors v1,v2,v3 are coplanar. Thus you can pick any two of these vectors to generate the exact same plane.
As an example: you start with vectors (1,0,0) and (0,1,0). Then you check several other vectors and find that two of them are coplanar to your initial vectors (for example (1,1,0) and (-1,-1,0)). This means that for your list of unique planes, you may add the plane generated by any two out of these four vectors, but not add any other combination of these.
Note: this works for finding 2 dimensional planes in higher dimensions as well of course. You can even check for higher dimensional planes, but this needs some adapting in the number of vectors you compare and the rank for which you check.

Clustering Data in a 3D matrix with another matrix

I Have got 2 Data cubes represented as 3D matrices. Both of them will be of same dimensions. we have to do rule based ordering. our condition is that if any sub cube of both of them ( sub cube must match exactly in location and orientation) matches atleast p% we can tell that they are similar. now given two 3D matrices containing the data , we have to write an algorithm which prints the number of similar subcubes that are similar in the given two cubes.
I tried brute force algorithm but it turned out to be very slow on large data sets. Is there any specific algorithm I can use here or any technique??
Thanks in advance.
We can adapt the first solution in this question. Construct another 3D matrix called count and fill all its edge cells corresponding to matching data with 1s. Then, starting from count(1,1,1), consider the cells in lexicographic order and set the count(i, j, k) for i,j,k such that the data matches to the smallest value of any of its neighbours which have already been set. If the data doesn't match, set count(i, j, k) = 0.
At the end, the non-zero elements of count contain the matching cubes, and their value denotes the width of the cube.

3D FFT decomposition in 2D FFT

Basically I am solving the diffusion equation in 3D using FFT and one of the ways to parallelise this is to decompose the 3D FFT in 2D FFTs.
As described in this paper: https://cmb.ornl.gov/members/z8g/csproject-report.pdf
The way to decompose a 3d fft would be by doing:
2d fft in xy direction
global transpose
1d fft in z direction
Basically, my problem is that I am not sure how to do this global transpose (as I assume it's transposing a 3d array I suppose). Anyone has came accross this? Thanks a lot.
Think of a 3d cube with nx*ny*nz elements. The 3d FFT of these elements is mathematically 3 stages of 1-d FFTs, one along each axis:
Do ny*nz transforms along the X axis, each transform handles nx elements
nx*nz transforms along the Y axis
nx*ny transforms along the Z axis
More generally, an N-dimensional FFT (N>1) is composed of many (N-1)-dimensional FFTs along that axis.
If the signal is real and you have an FFT that can return the half spectrum, then stage 1 would be about half as expensive (real FFT is cheaper), the remaining stages need to be complex, but they only need to have about half as many transforms. So the cost is roughly half.
If your 1d FFT can read input elements that are strided and pack the output into a contiguous buffer, then you end up doing a transposition at each stage.
This is how kissfft performs multi-dimensional FFTs.
P.S. When I need to get a mental pictures of higher dimensions, I think of:
sheets of paper with matrices of numbers (2d), in folders of numbered papers (3d), in numbered filing cabinets (4d), in numbered rooms (5d), in numbered buildings (6d), and so on ... So I can visualize the "filing cabinet" dimension
The "global transposition" mentioned in the paper is not a mathematical operation, but a rearrangement of data between the distributed memory machines.
The data calculated on one machine in step 1 has to be transferred to all other machines, vice versa, for step to. It has nothing to do with a matrix transposition.

Calculate covariance of two images in matlab

I am doing a project on image quality assessment. I converted the image to grayscale & divided the entire image into 8x8 matrices using mat2cell function. I did this for two images, and now I want to calculate covariance between these two images (i.e the covariance between the matrix of image 1 and covariance between the same matrix of image 2). Note that both are the same images: one a pure image without distortions and one with distortions.
First convert your image to matrix:
I = double(imread('photo.jpg'));
then calculate covariance:
x=cov(I);
For single matrix input, C has size [size(A,2) size(A,2)] based on the number of random variables (columns) represented by A. The variances of the columns are along the diagonal. If A is a row or column vector, C is the scalar-valued variance.
For two-vector or two-matrix input, C is the 2-by-2 covariance matrix between the two random variables. The variances are along the diagonal of C.

Resources