MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在MATLAB中的数组中保存多个函数? (https://www.labfans.com/bbs/showthread.php?t=24187)

poster 2019-12-10 20:48

如何在MATLAB中的数组中保存多个函数?
 
我是MATLAB的新手,所以我什至不知道这样做是否可行,但是这是...我正在尝试使用plot函数在单个图形中打印多条线。问题是,我希望能够通过简单地更改变量来指定图形应显示的行数,例如:{这是我想做的伪代码}

number_of_lines = 4; x = 0:0.5:5; function_output[number_of_lines]; for n=0:number_of_lines function_output[n] = sin(n + x); end for n=0:number_of_lines plot(x,function_output[n]); end 我知道上面的伪代码不完全是MATLAB,但是我想知道是否可以在MATLAB中执行这种算法。



[B]回答:[/B]

这是在MATLAB中实现示例的一种方法:

function_output = zeros(numel(x), number_of_lines); % Initialize a 2-D array for n = 1:number_of_lines % MATLAB uses 1-based indexing function_output(:, n) = sin(n + x).'; %' Compute a row vector, transpose % it into a column vector, and % place the data in a column of % the 2-D array end plot(x, function_output); % This will plot one line per column of the array 这里是一些文档链接,您应该阅读这些文档链接以学习和理解上面的代码:
[LIST][*] [URL="http://www.mathworks.com/help/matlab/math/creating-and-concatenating-matrices.html"]矩阵与数组[/URL][*] [URL="http://www.mathworks.com/help/matlab/math/matrix-indexing.html"]矩阵索引[/URL][*] [URL="http://www.mathworks.com/help/matlab/arithmetic-operators.html"]算术运算符[/URL][*] [URL="https://www.mathworks.com/help/matlab/ref/plot.html"]plot[/URL]功能[/LIST]
[url=https://stackoverflow.com/questions/4357671]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:12

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.