How do I crop a matrix within desired numerical limits?
9 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
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
0 Comments
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!