# How do I crop a matrix within desired numerical limits?

7 visualizzazioni (ultimi 30 giorni)
Goncalo Costa il 15 Mar 2023
Risposto: the cyclist il 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.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

David Hill il 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

the cyclist il 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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Denoising and Compression in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by