Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
GUI performance optimisation

Subject: GUI performance optimisation

From: Frédéric Bergeron

Date: 11 Aug, 2010 19:19:05

Message: 1 of 3

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!!

Subject: GUI performance optimisation

From: Sean

Date: 11 Aug, 2010 19:43:05

Message: 2 of 3

"Frédéric Bergeron" <frederic.bergeron@logiag.com> wrote in message <i3ut39$7el$1@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)

Subject: GUI performance optimisation

From: Frédéric Bergeron

Date: 11 Aug, 2010 20:49:05

Message: 3 of 3

Thanks Sean!
Fred

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us