arrays - 如何在 MATLAB 的数组中保存多个函数?

标签 arrays matlab plot

我是 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 中完成。

最佳答案

这是在 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

这里有一些文档链接,您应该通读以学习和理解上述代码:

关于arrays - 如何在 MATLAB 的数组中保存多个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4357671/

相关文章:

PHP PDO 产生 "Array"值而不是实际值

javascript - 推送到数组的数据不会保留在数组中

matlab - 将 "or"函数应用于两个以上的向量 Matlab

python - 在同一图中绘制两个函数,在两个 x Axis 上具有不同的比例

c# - 使用包含另一个对象数组的对象的反射读取属性

arrays - 如何替换数组中的重复元素?

matlab - MATLAB 中 [] 和 [1x0] 的区别

matlab - 不定大小-试图了解双三次插值

r - R 条形图的组着色错误

r - Ggplot2 Boxplot 宽度设置更改 x Axis