Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does MATLAB draw a picture

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report