Search code examples
matlabsparse-matrix

MATLAB: Convert two array to a sparse matrix


I'm looking for an a command or trick to convert two arrays to a sparse matrix. The two arrays contain x-values and y-values, which gives a coordinate in the cartesian coordinate system. I want to group the coordinates, which if the value is between some value on the x-axes and the y-axes.

% MATLAB
x_i = find(x > 0.1 & x < 0.9);
y_i = find(y > 0.4 & y < 0.8);

%Then I want to find indicies which are located in both x_i and y_i

Is there an easy way to this little trick?


Solution

  • Assuming that x and y have the same shape (which they should if they're coordinates), you can simply write

    commonIndices = find(x > 0.1 & x < 0.9 & y > 0.4 & y < 0.8);
    

    If you want a general way to find numbers that are common to two lists, you can use intersect

    commonEntries = intersect(x_i,y_i);