Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does MATLAB draw a picture

Shulou Source: shulou.com Published: 2022-06-01 06:30:18 10月18日 Update

This article mainly introduces the MATLAB how to draw, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

I need to do something recently, and one of the steps is to draw a picture like this. As shown above, I want to connect the red squares on the left with all the blue circles on the right. Normally we use a two-layer for loop and then plot, but this push will show you how to use arrayfun to draw the diagram. (arrayfun is nested in arrayfun)

In fact, later, I counted the running time of using for loop and arrayfun, and found that the time consuming of these two methods is basically the same, and even the time consuming of using for loop is a little less than that of using arrayfun.

But use arrayfun to be more concise.

Before you look at the program, review arrayfun first.

| |

| |

V

Avoid useless loops-- the other three functions of the fun series

Code:

Clear

Clc

Close all

Enter the coordinates of the device

In_x = zeros (1,10)

In_y = linspace (0,100,10)

% output the coordinates of the device

Out_x = 200.*ones (1,30)

Out_y = linspace (0,100,30)

% method 1:arrayfun

Tic

Figure (1)

Ax = axes ('NextPlot',' add')

F = @ (x, y) arrayfun (@ (x_out, y_out) plot ([x, x_out], [y, y_out]), out_x, out_y)

Arrayfun (f, in_x, in_y)

Toc

% method 2:for loop

Tic

Figure (2)

Ax2 = axes ('NextPlot',' add')

For I = 1: length (in_x)

For j = 1: length (out_x)

Plot ([in_x (I) out_x (j)], [in_y (I) out_y (j)])

End

End

Toc

Using arrayfun's method, those two sentences are explained as follows:

In fact, using a layer of for loop and then using an arrayfun will double the speed. As in method 3 below, I use arrayfun to nest arrayfun just to make fun of it. But it is really simple for arrayfun to nest another arrayfun.

But the readability is reduced, and it doesn't run well!

Method 3: loop a layer of for and then use an arrayfun

Tic

Figure (3)

Ax3 = axes ('NextPlot',' add')

For I = 1: length (in_x)

F = @ (x, y) line ([in_x (I), x], [in_y (I), y])

Arrayfun (f, out_x, out_y)

End

Toc

The running time of the three methods is as follows:

I changed the input points to 100 and the output points to 300!

Thank you for reading this article carefully. I hope the article "how to draw MATLAB" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Methods loops articles runs simplicity coordinates reality time points devices inputs outputs three above unpleasant things code value interests functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi vpn Linux Huawei