ans =

**Pull up a chair!**

Discussions is **your** place to get to know your peers, tackle the bigger challenges together, and have fun along the way.

- Want to see the latest updates? Follow the Highlights!
- Looking for techniques improve your MATLAB or Simulink skills? Tips & Tricks has you covered!
- Sharing the perfect math joke, pun, or meme? Look no further than Fun!
- Think there's a channel we need? Tell us more in Ideas

# Updated Discussions

In the spirit of warming up for this year's minihack contest, I'm uploading a walkthrough for how to design an airship using pure Matlab script. This is commented and uncondensed; half of the challenge for the minihacks is how minimize characters. But, maybe it will give people some ideas.

The actual airship design is from one of my favorite original NES games that I played when I was a kid - Little Nemo: The Dream Master. The design comes from the intro of the game when Nemo sees the Slumberland airship leave for Slumberland:

(Snip from a frame of the opening scene in Capcom's game Little Nemo: The Dream Master, showing the Slumberland airship).

I spent hours playing this game with my two sisters, when we were little. It's fun and tough, but the graphics sparked the imagination. On to the code walkthrough, beginning with the color palette: these four colors are the only colors used for the airship:

c1=cat(3,1,.7,.4); % Cream color

c2=cat(3,.7,.1,.3); % Magenta

c3=cat(3,0.7,.5,.1); % Gold

c4=cat(3,.5,.3,0); % bronze

We start with the airship carriage body. We want something rectangular but smoothed on the corners. To do this we are going to start with the separate derivatives of the x and y components, which can be expressed using separate blocks of only three levels: [1, 0, -1]. You could integrate to create a rectangle, but if we smooth the derivatives prior to integrating we will get rounded edges. This is done in the following code:

% Binary components for x & y vectors

z=zeros(1,30);

o=ones(1,100);

% X and y vectors

x=[z,o,z,-o];

y=[1+z,1-o,z-1,1-o];

% Smoother function (fourier / circular)

s=@(x)ifft(fft(x).*conj(fft(hann(45)'/22,260)));

% Integrator function with replication and smoothing to form mesh matrices

u=@(x)repmat(cumsum(s(x)),[30,1]);

% Construct x and y components of carriage with offsets

x3=u(x)-49.35;

y3=u(y)+6.35;

y3 = y3*1.25; % Make it a little fatter

% Add a z-component to make the full set of matrices for creating a 3D

% surface:

z3=linspace(0,1,30)'.*ones(1,260)*30;

z3(14,:)=z3(15,:); % These two lines are for adding platforms

z3(2,:)=z3(3,:); % to the carriage, later.

Plotting x, y, and the top row of the smoothed, integrated, and replicated matrices x3 and y3 gives the following:

We now have the x and y components for a 3D mesh of the carriage, let's make it more interesting by adding a color scheme including doors, and texture for the trim around the doors. Let's also add platforms beneath the doors for passengers to walk on.

Starting with the color values, let's make doors by convolving points in a color-matrix by a door shaped function.

m=0*z3; % Image matrix that will be overlayed on carriage surface

m(7,10:12:end)=1; % Door locations (lower deck)

m(21,10:12:end)=1; % Door locations (upper deck)

drs = ones(9, 5); % Door shape

m=1-conv2(m,ones(9,5),'same'); % Applying

To add the trim, we will convolve matrix "m" (the color matrix) with a square function, and look for values that lie between the extrema. We will use this to create a displacement function that bumps out the -x, and -y values of the carriage surface in intermediary polar coordinate format.

rm=conv2(m,ones(5)/25,'same'); % Smoothing the door function

rm(~m)=0; % Preserving only the region around the doors

rds=0*m; % Radial displacement function

rds(rm<1&rm>0)=1; % Preserving region around doors

rds(m==0)=0;

rds(13:14,:)=6; % Adding walkways

rds(1:2,:)=6;

% Apply radial displacement function

[th,rd]=cart2pol(x3,y3);

[x3T,y3T]=pol2cart(th,(rd+rds)*.89);

If we plot the color function (m) and radial displacement function (rds) we get the following:

In the upper plot you can see the doors, and in the bottom map you can see the walk way and door trim.

Next, we are going to add some flags draped from the bottom and top of the carriage. We are going to recycle the values in "z3" to do this, by multiplying that matrix with the absolute value of a sine-wave, squished a bit with the soft-clip erf() function.

We add a keel to the airship carriage using a canonical sphere turned on its side, again using the soft-clip erf() function to make it roughly rectangular in x and y, and multiplying with a vector that is half nan's to make the top half transparent.

At this point, since we are beginning the plotting of the ship, we also need to create our hgtransform objects. These allow us to move all of the components of the airship in unison, and also link objects with pivot points to the airship, such as the propeller.

% Now we need some flags extending around the top and bottom of the

% carriage. We can do this my multiplying the height function (z3) with the

% absolute value of a sine-wave, rounded with a compression function

% (erf() in this case);

g=-z3.*erf(abs(sin(linspace(0,40*pi,260))))/4; % Flags

% Also going to add a slight taper to the carriage... gives it a nice look

tp=linspace(1.05,1,30)';

% Finally, plotting. Plot the carriage with the color-map for the doors in

% the cream color, than the flags in magenta. Attach them both to transform

% objects for movement.

% Set up transform objects. 2 moving parts:

% 1) The airship itself and all sub-components

% 2) The propellor, which attaches to the airship and spins on its axis.

hold on;

P=hgtransform('Parent',gca); % Ship

S=hgtransform('Parent',P); % Prop

surf(x3T.*tp,y3T,z3,c1.*m,'Parent',P);

surf(x3,y3,g,c2.*rd./rd, 'Parent', P);

surf(x3,y3,g+31,c2.*rd./rd, 'Parent', P);

axis equal

% Now add the keel of the airship. Will use a canonical sphere and the

% erf() compression function to square off.

[x,y,z]=sphere(99);

mk=round(linspace(-1,1).^2+.3); % This function makes the top half of the sphere nan's for transparency.

surf(50*erf(1.4*z),15*erf(1.4*y),13*x.*mk./mk-1,.5*c2.*z./z, 'Parent', P);

% The carriage is done. Now we can make the blimp above it.

We haven't adjusted the shading of the image yet, but you can see the design features that have been created:

Next, we start working on the blimp. This is going to use a few more vertices & faces. We are going to use a tapered cylinder for this part, and will start by making the overlaid image, which will have 2 colors plus radial rings, circles, and squiggles for ornamentation.

M=525; % Blimp (matrix dimensions)

N=700;

% Assign the blimp the cream and magenta colors

t=122; % Transition point

b=ones(M,N,3); % Blimp color map template

bc=b.*c1; % Blimp color map

bc(:,t+1:end-t,:)=b(:,t+1:end-t,:).*c2;

% Add axial rings around blimp

l=[.17,.3,.31,.49];

l=round([l,1-fliplr(l)]*N); % Mirroring

lnw=ones(1,N); % Mask

lnw(l)=0;

lnw=rescale(conv(lnw,hann(7)','same'));

bc=bc.*lnw;

% Now add squiggles. We're going to do this by making an even function in

% the x-dimension (N, 725) added with a sinusoidal oscillation in the

% y-dimension (M, 500), then thresholding.

r=sin(linspace(0, 2*pi, M)*10)'+(linspace(-1, 1, N).^6-.18)*15;

q=abs(r)>.15;

r=sin(linspace(0, 2*pi, M)*12)'+(abs(linspace(-1, 1, N))-.25)*15;

q=q.*(abs(r)>.15);

% Now add the circles on the blimp. These will be spaced evenly in the

% polar angle dimension around the axis. We will have 9. To make the

% circles, we will create a cone function with a peak at the center of the

% circle, and use thresholding to create a ring of appropriate radius.

hs=[1,.75,.5,.25,0,-.25,-.5,-.75,-1]; % Axial spacing of rings

% Cone generation and ring loop

xy= @(h,s)meshgrid(linspace(-1, 1, N)+s*.53,(linspace(-1, 1, M)+h)*1.15);

w=@(x,y)sqrt(x.^2+y.^2);

for n=1:9

h=hs(n);

[xx,yy]=xy(h,-1);

r1=w(xx,yy);

[xx,yy]=xy(h,1);

r2=w(xx,yy);

b=@(x,y)abs(y-x)>.005;

q=q.*b(.1,r1).*b(.075,r1).*b(.1,r2).*b(.075,r2);

end

The figures below show the color scheme and mask used to apply the squiggles and circles generated in the code above:

Finally, for the colormap we are going to smooth the binary mask to avoid hard transitions, and use it to to add a "puffy" texture to the blimp shape. This will be done by diffusing the mask iteratively in a loop with a non-linear min() operator.

% 2D convolution function

ff=@(x)circshift(ifft2(fft2(x).*conj(fft2(hann(7)*hann(7)'/9,M,N))),[3,3]);

q=ff(q); % Smooth our mask function

hh=rgb2hsv(q.*bc); % Convert to hsv: we are going to use the value

% component for radial displacement offsets of the

% 3D blimp structure.

rd=hh(:,:,3); % Value component

for n=1:10

rd=min(rd,ff(rd)); % Diffusing the value component for a puffy look

end

rd=(rd+35)/36; % Make displacements effects small

% Now make 3D blimp manifold using "cylinder" canonical shape

[x,y,z]=cylinder(erf(sin(linspace(0,pi,N)).^.5)/4,M-1); % First argument is the blimp taper

[t,r]=cart2pol(x, y);

[x2,y2]=pol2cart(t, r.*rd'); % Applying radial displacment from mask

s=200;

% Plotting the blimp

surf(z'*s-s/2, y2'*s, x2'*s+s/3.9+15, q.*bc,'Parent',P);

Notice that the parent of the blimp surface plot is the same as the carriage (e.g. hgtransform object "P"). Plotting at this point using flat shading and adding some lighting gives the image below:

Next, we need to add a propeller so it can move. This will include the creation of a shaft using the cylinder() function. The rest of the pieces (the propeller blades, collars and shaft tip) all use the same canonical sphere with distortions applied using various math functions. Note that when the propeller is made it is linked to hgtransform object "S" rather than "P." This will allow the propeller to rotate, but still be joined to the airship.

% Next, the propeller. First, we start with the shaft. This is a simple

% cylinder. We add an offset variable and a scale variable to move our

% propeller components around, as well.

shx = -70; % This is our x-shifter for components

scl = 3; % Component size scaler

[x,y,z]=cylinder(1, 20); % Canonical cylinder for prop shaft.

p(1)=surf(-scl*(z-1)*7+shx,scl*x/2,scl*y/2,0*x+c4,'Parent',P); % Prop shaft

% Now the propeller. This is going to be made from a distorted sphere.

% The important thing here is that it is linked to the "S" hgtransform

% object, which will allow it to rotate.

[x,y,z]=sphere(50);

a=(-1:.04:1)';

x2=(x.*cos(a)-y/3.*sin(a)).*(abs(sin(a*2))*2+.1);

y2=(x.*sin(a)+y/3.*cos(a));

p(2)=surf(-scl*y2+shx,scl*x2,scl*z*6,0*x+c3,'Parent',S);

% Now for the prop-collars. You can see these on the shaft in the NES

% animation. These will just be made by using the canonical sphere and the

% erf() activation function to square it in the x-dimension.

g=erf(z*3)/3;

r=@(g)surf(-scl*g+shx,scl*x,scl*y,0*x+c3,'Parent',P);

r(g);

r(g-2.8);

r(g-3.7);

% Finally, the prop shaft tip. This will just be the sphere with a

% taper-function applied radially.

t=1.7*cos(0:.026:1.3)'.^2;

p(3)=surf(-(z*2+2)*scl + shx,x.*t*scl,y.*t*scl, 0*x+c4,'Parent',P);

Now for some final details including the ropes to the blimp, a flag hung on one of the ropes, and railings around the walkways so that passengers don't plummet to their doom. This will make use of the ad-hoc "ropeG" function, which takes a 3D vector of points and makes a conforming cylinder around it, so that you get lighting functions etc. that don't work on simple lines. This function is added to the script at the end to do this:

% Rope function for making a 3D curve have thickness, like a rope.

% Inputs:

% - xyz (3D curve vector, M points in 3 x M format)

% - N (Number of radial points in cylinder function around the curve

% - W (Width of the rope)

%

% Outputs:

% - xf, yf, zf (Matrices that can be used with surf())

function [xf, yf, zf] = RopeG(xyz, N, W)

% Canonical cylinder with N points in circumference

[xt,yt,zt] = cylinder(1, N);

% Extract just the first ring and make (W) wide

xyzt = [xt(1, :); yt(1, :); zt(1, :)]*W;

% Get local orientation vector between adjacent points in rope

dxyz = xyz(:, 2:end) - xyz(:, 1:end-1);

dxyz(:, end+1) = dxyz(:, end);

vcs = dxyz./vecnorm(dxyz);

% We need to orient circle so that its plane normal is parallel to

% xyzt. This is a kludgey way to do that.

vcs2 = [ones(2, size(vcs, 2)); -(vcs(1, :) + vcs(2, :))./(vcs(3, :)+0.01)];

vcs2 = vcs2./vecnorm(vcs2);

vcs3 = cross(vcs, vcs2);

p = @(x)permute(x, [1, 3, 2]);

rmats = [p(vcs3), p(vcs2), p(vcs)];

% Create surface

xyzF = pagemtimes(rmats, xyzt) + permute(xyz, [1, 3, 2]);

% Outputs for surf format

xf = squeeze(xyzF(1, :, :));

yf = squeeze(xyzF(2, :, :));

zf = squeeze(xyzF(3, :, :));

end

Using this function we can define the ropes and balconies. Note that the balconies simply recycle one of the rows of the original carriage surface, defining the outer rim of the walkway, but bumping up in the z-dimension.

cb=-sqrt(1-linspace(1, 0, 100).^2)';

c1v=[linspace(-67, -51)', 0*ones(100,1),cb*30+35];

c2v=[c1v(:,1),c1v(:,2),(linspace(1,0).^1.5-1)'*15+33];

c3v=c2v.*[-1,1,1];

[xr,yr,zr]=RopeG(c1v', 10, .5);

surf(xr,yr,zr,0*xr+c2,'Parent',P);

[xr,yr,zr]=RopeG(c2v', 10, .5);

surf(xr,yr,zr,0*zr+c2,'Parent',P);

[xr,yr,zr]=RopeG(c3v', 10, .5);

surf(xr,yr,zr,0*zr+c2,'Parent',P);

% Finally, balconies would add a nice touch to the carriage keep people

% from falling to their death at 10,000 feet.

[rx,ry,rz]=RopeG([x3T(14, :); y3T(14,:); 0*x3T(14,:)+18]*1.01, 10, 1);

surf(rx,ry,rz,0*rz+cat(3,0.7,.5,.1),'Parent',P);

surf(rx,ry,rz-13,0*rz+cat(3,0.7,.5,.1),'Parent',P);

And, very last, we are going to add a flag attached to the outer cable. Let's make it flap in the wind. To make it we will recycle the z3 matrix again, but taper it based on its x-value. Then we will sinusoidally oscillate the flag in the y dimension as a function of x, constraining the y-position to be zero where it meets the cable. Lastly, we will displace it quadratically in the x-dimension as a function of z so that it lines up nicely with the rope. The phase of the sine-function is modified in the animation loop to give it a flapping motion.

h=linspace(0,1);

sc=10;

[fx,fz]=meshgrid(h,h-.5);

F=surf(sc*2.5*fx-90-2*(fz+.5).^2,sc*.3*erf(3*(1-h)).*sin(10*fx+n/5),sc*fz.*h+25,0*fx+c3,'Parent',P);

Plotting just the cables and flag shows:

Putting all the pieces together reveals the full airship:

A note about lighting: lighting and material properties really change the feel of the image you create. The above picture is rendered in a cartoony style by setting the specular exponent to a very low value (1), and adding lots of diffuse and ambient reflectivity as well. A light below the airship was also added, albeit with lower strength. Settings used for this plot were:

shading flat

view([0, 0]);

L=light;

L.Color = [1,1,1]/4;

light('position', [0, 0.5, 1], 'color', [1,1,1]);

light('position', [0, 1, -1], 'color', [1, 1, 1]/5);

material([1, 1, .7, 1])

set(gcf, 'color', 'k');

axis equal off

What about all the rest of the stuff (clouds, moon, atmospheric haze etc.) These were all (mostly) recycled bits from previous minihack entries. The clouds were made using power-law noise as explained in Adam Danz' blog post. The moon was borrowed from moonrun, but with an increased number of points. Atmospheric haze was recycled from Matlon5. The rest is just camera angles, hgtransform matrix updates, and updating alpha-maps or vertex coordinates.

Finally, the use of hann() adds the signal processing toolbox as a dependency. To avoid this use the following anonymous function:

hann = @(x)-cospi(linspace(0,2,x)')/2+.5;

Local large language models (LLMs), such as llama, phi3, and mistral, are now available in the Large Language Models (LLMs) with MATLAB repository through Ollama™!

Read about it here:

With the release of MATLAB R2023a, we also are giving you early access to the new MATLAB Desktop. This includes Dark Mode! Download via Add-on Explorer in MATLAB or find here on File Exchange.

These are some of the many enhancements and new features of the new desktop:

- Dark mode / theme support - Change the colors of the desktop by selecting a dark theme.
- Updated layout - Quickly access tools and change your layout using the sidebar.
- Expanded search capabilities - Easily find actions, settings, and resources using the new universal search box.
- Figure toolstrip - Use the new figure toolstrip to easily modify annotations, text, and line styles, and view generated code.
- Keyboard and screen reader accessibility support - Use a screen reader to interact with the Command Window, Editor and desktop tools.
- Enhanced MATLAB Projects workflows - Collaborate using an improved user interface, manage multiple repositories using the Branch Manager, and investigate project hierarchies using the new Dependency Analyzer view.

Have fun trying it out and let us know what you think!

Hello everyone,

Over the past few weeks, our community members have shared some incredible insights and resources. Here are some highlights worth checking out:

### Interesting Questions

Johnathan is seeking help with implementing a complex equation into MATLAB's curve fitting toolbox. If you have experience with curve fitting or MATLAB, your input could be invaluable!

### Popular Discussions

Athanasios continues his exploration of the Duffing Equation, delving into its chaotic behavior. It's a fascinating read for anyone interested in nonlinear dynamics or chaos theory.

John shares his playful exploration with MATLAB to find a generative equation for a sequence involving Fibonacci numbers. It's an intriguing challenge for those who love mathematical puzzles.

### From File Exchange

Ayesha provides a graphical analysis of linearised models in epidemiology, offering a detailed look at the dynamics of these systems. This resource is perfect for those interested in mathematical modeling.

Gareth brings some humor to MATLAB with a toolbox designed to share jokes. It's a fun way to lighten the mood during conferences or meetups.

### From the Blogs

Ned Gulley interviews Tim Marston, the 2023 MATLAB Mini Hack contest winner. Tim's creativity and skills are truly inspiring, and his story is a must-read for aspiring programmers.

Sivylla discusses the integration of AI with embedded systems, highlighting the benefits of using MATLAB and Simulink. It's an insightful read for anyone interested in the future of AI technology.

Thank you to all our contributors for sharing your knowledge and creativity. We encourage everyone to engage with these posts and continue fostering a vibrant and supportive community.

Happy exploring!

Similar to what has happened with the wishlist threads (#1 #2 #3 #4 #5), the "what frustrates you about MATLAB" thread has become very large. This makes navigation difficult and increases page load times.

So here is the follow-up page.

What should you post where?

Next Gen threads (#1): features that would break compatibility with previous versions, but would be nice to have

@anyone posting a new thread when the last one gets too large (about 50 answers seems a reasonable limit per thread), please update this list in all last threads. (if you don't have editing privileges, just post a comment asking someone to do the edit)

Explore the newest online training courses, available as of 2024b: one new Onramp, eight new short courses, and one new learning path. Yes, that’s 10 new offerings. We’ve been busy.

As a reminder, Onramps are free to all. Short courses and learning paths require a subscription to the Online Training Suite (OTS).

- Multibody Simulation Onramp
- Analyzing Results in Simulink
- Battery Pack Modeling
- Introduction to Motor Control
- Signal Processing Techniques for Streaming Signals
- Core Signal Processing Techniques in MATLAB (learning path – includes the four short courses listed below)

syms u v

atan2alt(v,u)

function Z = atan2alt(V,U)

% extension of atan2(V,U) into the complex plane

Z = -1i*log((U+1i*V)./sqrt(U.^2+V.^2));

% check for purely real input. if so, zero out the imaginary part.

realInputs = (imag(U) == 0) & (imag(V) == 0);

Z(realInputs) = real(Z(realInputs));

end

As I am editing this post, I see the expected symbolic display in the nice form as have grown to love. However, when I save the post, it does not display. (In fact, it shows up here in the discussions post.) This seems to be a new problem, as I have not seen that failure mode in the past.

You can see the problem in this Answer forum response of mine, where it did fail.

Hot off the heels of my High Performance Computing experience in the Czech republic, I've just booked my flights to Atlanta for this year's supercomputing conference at SC24.

Will any of you be there?

RGB triplet [0,1]

9%

RGB triplet [0,255]

12%

Hexadecimal Color Code

13%

Indexed color

16%

Truecolor array

37%

Equally unfamiliar with all-above

13%

2398 votes

Create a struct arrays where each struct has field names "a," "b," and "c," which store different types of data. What efficient methods do you have to assign values from individual variables "a," "b," and "c" to each struct element? Here are five methods I've provided, listed in order of decreasing efficiency. What do you think?

Create an array of 10,000 structures, each containing each of the elements corresponding to the a,b,c variables.

num = 10000;

a = (1:num)';

b = string(a);

c = rand(3,3,num);

Here are the methods;

%% method1

t1 =tic;

s = struct("a",[], ...

"b",[], ...

"c",[]);

s1 = repmat(s,num,1);

for i = 1:num

s1(i).a = a(i);

s1(i).b = b(i);

s1(i).c = c(:,:,i);

end

t1 = toc(t1);

%% method2

t2 =tic;

for i = num:-1:1

s2(i).a = a(i);

s2(i).b = b(i);

s2(i).c = c(:,:,i);

end

t2 = toc(t2);

%% method3

t3 =tic;

for i = 1:num

s3(i).a = a(i);

s3(i).b = b(i);

s3(i).c = c(:,:,i);

end

t3 = toc(t3);

%% method4

t4 =tic;

ct = permute(c,[3,2,1]);

t = table(a,b,ct);

s4 = table2struct(t);

t4 = toc(t4);

%% method5

t5 =tic;

s5 = struct("a",num2cell(a),...

"b",num2cell(b),...

"c",squeeze(mat2cell(c,3,3,ones(num,1))));

t5 = toc(t5);

%% plot

bar([t1,t2,t3,t4,t5])

xtickformat('method %g')

ylabel("time(second)")

yline(mean([t1,t2,t3,t4,t5]))

In the past two years, MATHWORKS has updated the image viewer and audio viewer, giving them a more modern interface with features like play, pause, fast forward, and some interactive tools that are more commonly found in typical third-party players. However, the video player has not seen any updates. For instance, the Video Viewer or vision.VideoPlayer could benefit from a more modern player interface. Perhaps I haven't found a suitable built-in player yet. It would be great if there were support for custom image processing and audio processing algorithms that could be played in a more modern interface in real time.

Additionally, I found it quite challenging to develop a modern video player from scratch in App Designer.（If there's a video component for that that would be great）

-----------------------------------------------------------------------------------------------------------------

BTW，the following picture shows the built-in function uihtml function showing a more modern playback interface with controls for play, pause and so on. But can not add real-time image processing algorithms within it.

Dear MATLAB contest enthusiasts,

In the 2023 MATLAB Mini Hack Contest, Tim Marston captivated everyone with his incredible animations, showcasing both creativity and skill, ultimately earning him the 1st prize.

We had the pleasure of interviewing Tim to delve into his inspiring story. You can read the full interview on MathWorks Blogs: Community Q&A – Tim Marston.

Last question: Are you ready for this year’s Mini Hack contest?

As far as I know, starting from MATLAB R2024b, the documentation is defaulted to be accessed online. However, the problem is that every time I open the official online documentation through my browser, it defaults or forcibly redirects to the documentation hosted site for my current geographic location, often with multiple pop-up reminders, which is very annoying!

Suggestion: Could there be an option to set preferences linked to my personal account so that the documentation defaults to my chosen language preference without having to deal with “forced reminders” or “forced redirection” based on my geographic location? I prefer reading the English documentation, but the website automatically redirects me to the Chinese documentation due to my geolocation, which is quite frustrating!

Mari is helping Dad work.

Don't use / What are Projects?

26%

1–10

31%

11–20

15%

21–30

9%

31–50

7%

51+ (comment below)

12%

3579 votes

Swimming, diving

16%

Other water-based sport

4%

Gymnastics

20%

Other indoor arena sport

15%

track, field

24%

Other outdoor sport

21%

346 votes

Base case:

Suppose you need to do a computation many times. We are going to assume that this computation cannot be vectorized. The simplest case is to use a for loop:

number_of_elements = 1e6;

test_fcn = @(x) sqrt(x) / x;

tic

for i = 1:number_of_elements

x(i) = test_fcn(i);

end

t_forward = toc;

disp(t_forward + " seconds")

Preallocation:

This can easily be sped up by preallocating the variable that houses results:

tic

x = zeros(number_of_elements, 1);

for i = 1:number_of_elements

x(i) = test_fcn(i);

end

t_forward_prealloc = toc;

disp(t_forward_prealloc + " seconds")

In this example, preallocation speeds up the loop by a factor of about three to four (running in R2024a). Comment below if you get dramatically different results.

disp(sprintf("%.1f", t_forward / t_forward_prealloc))

Run it in reverse:

Is there a way to skip the explicit preallocation and still be fast? Indeed, there is.

clear x

tic

for i = number_of_elements:-1:1

x(i) = test_fcn(i);

end

t_backward = toc;

disp(t_backward + " seconds")

By running the loop backwards, the preallocation is implicitly performed during the first iteration and the loop runs in about the same time (within statistical noise):

disp(sprintf("%.2f", t_forward_prealloc / t_backward))

Do you get similar results when running this code? Let us know your thoughts in the comments below.

Beneficial side effect:

Have you ever had to use a for loop to delete elements from a vector? If so, keeping track of index offsets can be tricky, as deleting any element shifts all those that come after. By running the for loop in reverse, you don't need to worry about index offsets while deleting elements.