Indexing and Clearing for Multidimensional Array

1 view (last 30 days)
I'm generating data for a set of objects moving around in the XY plane (although they all have Z values as well). I have two 3-dimensional matrices that represent these objects: A & B. Matrix A represents one class of objects and Matrix B represents another class. For both of these:
1st dimension is the object index (for example, A could have 35 objects and B could have just 1)
2nd dimension contains the time steps
3rd dimension contains the X, Y, Z coordinates of each object
My aim is to calculate for each time step the distance from each object in A to the 1 object in B and if it's greater than max_range or less than min_range, I'd like to set the X, Y, and Z coordinates of that object at the given time to [0 0 0]. I wrote the code below which will set every X value I want to 0, but after trying many modifications I can't figure out how to make it set the corresponding Y and Z values to 0 as well.
A((sqrt((A(:, 1:end, 1) - B(1, 1:end, 1)).^2 + (A(:, 1:end, 2) - B(1, 1:end, 2)).^2) > max_range)) = 0;
A((sqrt((A(:, 1:end, 1) - B(1, 1:end, 1)).^2 + (A(:, 1:end, 2) - B(1, 1:end, 2)).^2)< min_range)) = 0;

Accepted Answer

Matt J
Matt J on 14 Dec 2017
Edited: Matt J on 14 Dec 2017
Here, I use scalar expansion features from R2016b,
dist=sum( (A-B(1,:,:)).^2 ,3);
keep=~( dist>max_range^2 | dist<min_range^2 );
A=A.*keep;

More Answers (0)

Categories

Find more on Linear Programming and Mixed-Integer Linear Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!