简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM