【问题标题】:Select and plot value above a threshold选择并绘制高于阈值的值
【发布时间】:2017-09-14 01:35:37
【问题描述】:

我有一个情节,其中有一些噪音成分。我计划从该图中选择最好高于阈值的数据,在我的情况下,我计划将其保持在 Y 轴上的 2.009。并绘制仅在其上方的线条。如果有什么低于我想把它绘制为0。 如图所示

t1=t(1:length(t)/5);  
t2=t(length(t)/5+1:2*length(t)/5);
t3=t(2*length(t)/5+1:3*length(t)/5);
t4=t(3*length(t)/5+1:4*length(t)/5);
t5=t(4*length(t)/5+1:end);
X=(length(prcdata(:,4))/5);
a = U(1 : X);
b = U(X+1: 2*X);
c = U(2*X+1 : 3*X);
d = U(3*X+1 : 4*X);
e = U(4*X+1 : 5*X);
figure;
subplot (3,2,2)
plot(t1,a);
subplot (3,2,3)
plot(t2,b);   
subplot(3,2,4)
plot(t3,c);
subplot(3,2,5)
plot(t4,d);
subplot(3,2,6)
plot(t5,e);
subplot(3,2,1)
plot(t,prcdata(:,5));
figure;
A=a(a>2.009,:);
plot (t1,A);

此代码将数据拆分(在图像中每 2.8 秒分成 5 个,我计划在前 2.8 秒内使用阈值。另外我还有另一个代码,但我不确定它是否有效,因为它需要很长时间待分析

figure;
A=a(a>2.009,:);
plot (t1,A);
for k=1:length(a)
    if a(k)>2.009
        plot(t1,a(k)), hold on
    else 
        plot(t1,0), hold on
    end
end
hold off

【问题讨论】:

    标签: matlab threshold


    【解决方案1】:

    问题在于,您可能会尝试绘制数千次,并在绘图上添加数千个点,这会导致计算机出现严重的内存和图形问题。您可以做的一件事是预处理所有信息,然后一次绘制所有信息,这将花费更少的时间。

    figure
    threshold = 2.009;
    A=a>threshold; %Finds all locations where the vector is above your threshold
    plot_vals = a.*A; %multiplies by logical vector, this sets invalid values to 0 and leaves valid values untouched
    plot(t1,plot_vals)
    

    由于 MATLAB 是一种高度矢量化的语言,这种格式不仅由于缺少 for 循环而计算速度更快,而且由于图形引擎不需要单独处理数千个点,因此它在您的计算机上的密集度也大大降低.

    MATLAB 处理绘图的方式是处理每一行。当您绘制一个向量时,MATLAB 能够简单地将向量存储在一个地址中,并在绘图时调用它一次。但是,当单独调用每个点时,MATLAB 必须将每个点存储在内存中的单独位置,并单独调用所有点,并以图形方式完全分别处理每个点。

    这里的每个请求是编辑 图(t1(A),plot_vals(A))

    【讨论】:

    • 谢谢你。使用这个你的意思是说它会更快?我会试试谢谢。
    • 当我运行 thsi 代码时出现错误 Error using .* 矩阵尺寸必须一致。 al 中的错误(第 62 行) plot_vals = a.*A; % 乘以逻辑向量,这会将无效值设置为 0 并保持有效值不变
    • 有没有办法消除这段代码中对应0的列和行?
    • 编辑了我的回复
    猜你喜欢
    • 1970-01-01
    • 2013-06-26
    • 2021-04-26
    • 1970-01-01
    • 2019-08-27
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多