Find first row/column with at least one finite element
1 view (last 30 days)
Show older comments
Hello :)
I am trying to crop an array to represent it with surf().
The region of interest of the image is always centered and the rest are NaNs, so there woulb be always regions at the top/bot and left/right with rows/columns composed by all NaNs.
I am trying to find the indexes of the first rows/columns containing at least one finite as the array changes depending on the image that is analysed.
This is what I have:
first_row = find(any(isfinite(phases)),1)
last_row = find(any(isfinite(phases)),1, 'last')
first_column = find(any(isfinite(phases)),2)
last_column = find(any(isfinite(phases)),2, 'last')
Then I crop:
surf(phases(first_row:last_row,first_column:last_column));
But I am not having the expected results.
THANK YOU IN ADVANCE.
Gonzalo.
0 Comments
Accepted Answer
KSSV
on 19 Jun 2020
Edited: KSSV
on 19 Jun 2020
clc; clear all ;
% demo data
A = NaN(100) ;
A(25:75,25:75) = rand(51) ;
% Get non=nan's index
idx = ~isnan(A) ;
% Make X, Y grid for demo
[X,Y] = meshgrid(1:100,1:100) ;
% find the first index non-NaN
id = find(idx(:)==1);
[r1,c1] = ind2sub(size(A),id(1)) ;
[r2,c2] = ind2sub(size(A),id(end)) ;
%
pcolor(A)
shading interp
hold on
plot(X(r1,c1),Y(r1,c1),'*r')
plot(X(r2,c2),Y(r2,c2),'*r')
4 Comments
KSSV
on 19 Jun 2020
You may try this:
Let X, Y,Z be your matrices.
idx = ~isnan(Z) ;
x = X(idx) ;
y = Y(idx) ;
z = Z(idx) ;
m = 100 ; n = 100 ;
xi = linspace(min(x),max(x),m) ;
yi = linspace(min(y),max(y),n) ;
[X,Y] = meshgrid(xi,yi) ;
Z = scatteredInterpolant(x,y,z,X,Y) ;
Or get the bounding box for the region which covers your required.
Pmin = [min(x) min(y)] ;
Pmax = [max(x) max(y)] ;
Now you have the bouning box, you can get what you want.
More Answers (0)
See Also
Categories
Find more on Line Plots 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!