From: Frédéric Bergeron on
Hi,

I'm creating a GUI to do field leveling.
My gui has surface plots (with the function surf) and the data I use comes from real agricultural fields. Therefore, I don't have «clean & square» matrices for x,y,z from my data. To use the surf function, I build square matrices of NaNs, then I put my x,y and z data in those matrices after. The surf function ignores the NaNs, so my plot looks exactly as the real field.

Problem:
I have data of more than 5000 points, and I think my code could be optimised for building the square matrices (especially the z one). I'm using a lot of for loops but I read that indexing could be more efficient, but I don't know how to adapt it so that the Z matrix can be builded in less time. Can somebody help me with that?

Code sample:

%Example of data
data=[1 1 3;1 2 3;1 3 4;
1 4 4.5;1 5 5;2 1 4.5;
2 2 5;2 3 6;2 4 5;
2 5 5;2 6 6;3 3 7;
3 4 6;3 5 7];
xall=data(:,1);
yall=data(:,2);
zall=data(:,3);

%Building "clean" matrices xgrid and ygrid for 3d plotting
Xmin=min(xall); Xmax=max(xall);
Ymin=min(yall); Ymax=max(yall);
x=Xmin:1:Xmax;
y=Ymin:1:Ymax;
[xgrid,ygrid]=meshgrid(x,y);

%Building the Z matrix for 3d plotting
%*** HERE I WANT TO IMPROVE MY CODE ***
%Is there a way to improve that part??? (indexing,etc...)
Zreel=NaN*ones(length(x),length(y));
for l=1:size(data,1)
for ii=1:length(x)
for j=1:length(y)
if data(l,1)==x(ii) && data(l,2)==y(j)
Zreel(ii,j)=data(l,3);
end
end
end
end
Zreel=Zreel';

%Plotting
hfig=figure;
hplot=surf(xgrid,ygrid,Zreel);

Thanks a lot!!
From: Sean on
"Frédéric Bergeron" <frederic.bergeron(a)logiag.com> wrote in message <i3ut39$7el$1(a)fred.mathworks.com>...
> Hi,
>
> I'm creating a GUI to do field leveling.
> My gui has surface plots (with the function surf) and the data I use comes from real agricultural fields. Therefore, I don't have «clean & square» matrices for x,y,z from my data. To use the surf function, I build square matrices of NaNs, then I put my x,y and z data in those matrices after. The surf function ignores the NaNs, so my plot looks exactly as the real field.
>
> Problem:
> I have data of more than 5000 points, and I think my code could be optimised for building the square matrices (especially the z one). I'm using a lot of for loops but I read that indexing could be more efficient, but I don't know how to adapt it so that the Z matrix can be builded in less time. Can somebody help me with that?
>
> Code sample:
>
> %Example of data
> data=[1 1 3;1 2 3;1 3 4;
> 1 4 4.5;1 5 5;2 1 4.5;
> 2 2 5;2 3 6;2 4 5;
> 2 5 5;2 6 6;3 3 7;
> 3 4 6;3 5 7];
> xall=data(:,1);
> yall=data(:,2);
> zall=data(:,3);
>
> %Building "clean" matrices xgrid and ygrid for 3d plotting
> Xmin=min(xall); Xmax=max(xall);
> Ymin=min(yall); Ymax=max(yall);
> x=Xmin:1:Xmax;
> y=Ymin:1:Ymax;
> [xgrid,ygrid]=meshgrid(x,y);
>
> %Building the Z matrix for 3d plotting
> %*** HERE I WANT TO IMPROVE MY CODE ***
> %Is there a way to improve that part??? (indexing,etc...)
> Zreel=NaN*ones(length(x),length(y));
> for l=1:size(data,1)
> for ii=1:length(x)
> for j=1:length(y)
> if data(l,1)==x(ii) && data(l,2)==y(j)
> Zreel(ii,j)=data(l,3);
> end
> end
> end
> end
> Zreel=Zreel';
>
> %Plotting
> hfig=figure;
> hplot=surf(xgrid,ygrid,Zreel);
>
> Thanks a lot!!

Sure:
%%%
Zreel2 = nan(length(x),length(y));
Zreel2(sub2ind(size(Zreel2),xall,yall)) = zall;
Zreel2 = Zreel2';

isequalwithequalnans(Zreel,Zreel2)
From: Frédéric Bergeron on
Thanks Sean!
Fred