【发布时间】:2020-11-16 01:52:52
【问题描述】:
我在 MATLAB 中使用命令跟踪了一个长度为 N 的离散信号 x
stem(abs(x)); axis([0 N+6 0 4]);
结果图如下:
我的问题我只需要一些对应于例如索引 [7 10 11 12 15 18 48 50 52 60] 的值以用不同的颜色着色,比如说用红色。 我怎样才能在我的身材中做到这一点?
【问题讨论】:
标签: matlab matlab-figure
我在 MATLAB 中使用命令跟踪了一个长度为 N 的离散信号 x
stem(abs(x)); axis([0 N+6 0 4]);
结果图如下:
我的问题我只需要一些对应于例如索引 [7 10 11 12 15 18 48 50 52 60] 的值以用不同的颜色着色,比如说用红色。 我怎样才能在我的身材中做到这一点?
【问题讨论】:
标签: matlab matlab-figure
hold on 和矩阵索引使用多个图您可以使用hold on 将绘图放置在绘图顶部。这确实需要进行调整,在这种情况下您需要一个向量 Sample 和 Indices 指定样本编号/数据点索引。也可以使用矩阵索引获取关键点对应的幅度/数据点,Indicies。
%Vector relating to the sample/data point number%
Sample = linspace(1,70,70);
%Random test data%
X = randi([0,2],1,70);
stem(Sample,X);
hold on
%Key indices to change colour%
Key_Indices = [7 10 11 12 15 18 48 50 52 60];
%Matrix indexing to get values/amplitudes corresponding to key indices%
X_Prime = X(Key_Indices);
stem(Key_Indices,X_Prime,'r');
axis([0 70 0 3]);
hold off
使用 MATLAB R2019b 运行
【讨论】:
这段代码只使圆圈变成红色,而不是茎
plot with selected red circles
%Vector relating to the sample/data point number
Sample = linspace(1,70,70);
%Random test data
X = randi([0,2],1,70);
stem(Sample,X);
%Key indices to change color
Key_Indices = [7 10 11 12 15 18 48 50 52 60];
line(Sample(Key_Indices), X(Key_Indices), 'linestyle', 'none', 'marker', 'o', 'color', 'r')
axis([0 70 0 3])
grid on
【讨论】: