How do I crop a matrix within desired numerical limits?
6 views (last 30 days)
Show older comments
Goncalo Costa on 15 Mar 2023
Answered: the cyclist on 15 Mar 2023
I have a 3D matrix representing an image (related to another question on the website), but I am only truly interested in a small part of that image. My matrix is a 271036 x 3 double structure (too big to upload here, even if zipped). More specifically, I am interested in the data contained within the limits:
z limits = [-2 1]
y limits = [-16 -9]
x limits = [-1 14]
I can plot the image within these limts, but how do I crop 3 dimensions according to the above limits? The cropped image looks like the figure below:
whilst the original can be seen in the aforemention question.
Thanks for your help.
David Hill on 15 Mar 2023
the cyclist on 15 Mar 2023
I am unclear what you mean by "crop" here. I can think of two possibilities:
- Remove rows of your matrix that have values outside those bounds.
- Keep the data in the matrix as it is, but change the axis limits.
If your matrix is M, and I am guessing correctly that the first column is x, etc, then for the first one, you can do
xlimit = [-1 14];
ylimit = [-16 -9];
zlimit = [-2 1];
xRemove = (M(:,1) < xlimit(1)) | (M(:,1) > xlimit(2));
yRemove = (M(:,2) < ylimit(1)) | (M(:,2) > ylimit(2));
zRemove = (M(:,3) < zlimit(1)) | (M(:,3) > zlimit(2));
anyRemove = xRemove | yRemove | zRemove;
M(anyRemove,:) = ;
For the second way, you just need to do something like
on those axes. The exact syntax will depend on the plotting method. You can also use the xlim (etc) command.
Find more on 3-D Scene Control 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!