How do I crop a matrix within desired numerical limits?

6 views (last 30 days)
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.

Answers (2)

David Hill
David Hill on 15 Mar 2023
M=[-2+16*rand(100,1),-17+9*rand(100,1),-3+5*rand(100,1)];
M(M(:,3)<-2|M(:,3)>1,:)=[];
M(M(:,2)<-16|M(:,2)>-9,:)=[];
M(M(:,1)<-1|M(:,1)>14,:)=[];
M
M = 39×3
7.5034 -11.0671 -0.4025 4.5679 -10.5011 -1.8194 10.8975 -12.8849 -0.8642 10.4339 -14.6191 -1.6638 9.7694 -12.0957 -0.7299 10.7378 -14.8615 -1.6498 2.1444 -15.4185 0.5519 1.6053 -10.5995 -1.3188 8.5561 -13.3561 0.5825 6.6685 -9.2076 -1.3074

the cyclist
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
set(gca,"XLim",xlimit,"YLim",ylimit,"ZLim",zlimit)
on those axes. The exact syntax will depend on the plotting method. You can also use the xlim (etc) command.

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!