消除与 MATLAB 中为零的列对应的行中的值
Eliminate the values in the rows corresponding to a column which are zero in MATLAB
我有一个要分析的数据矩阵。我有一个数据,我应用了一些处理部分,并且在尝试对其应用阈值时设法获得了低于某个水平的一些信息。所以在我应用阈值后,数据变为 0 点。所以我想知道是否有一种方法可以消除这些点而不会在它们之间留下 0。这就是图中带有零的样子
我试图在没有间隙的情况下绘制它,X 轴是时间,y 轴是振幅。那么是否可以只绘制蓝色的事件和在一起的时间?
%Find time
N = size(prcdata(:,1),1);
t=T*(0:N-1)';
figure;
plot(t,U);
t1=t(1:length(t)/5);
X=(length(prcdata(:,4))/5);
a = U(1 : X);
threshold=3.063;
A=a>threshold;
plot_vals=a.*A;
figure;
plot(t2,plot_vals1); %gives the plot which i added with this
我也试过这段代码来合并没有零的事件,但它给我的只是一个 0 处的直线图。
%% Eliminate the rows and colomns which are zero
B1=plot_vals1(plot_vals1 <= 0, :);
figure;
plot(B1);
请问还有什么方法可以把上图的散点取下来?将使用 scatter(t2,plot_vals1);工作?
So will that be possible to just plot the events which are in blue and the time together?
如果发生的时间对您来说不重要,那么以下方法可行:
在 A=a>threshold;
之后,将您的代码更改为
plot_vals=a(A);
figure;
plot(plot_vals);
如果发生时间很重要,那么您可以尝试使用绘图的 'XTick'
和 'XTickLabel'
属性以编程方式设置 x 刻度和标签。
像这样获取对应的兴趣时间:
t2=t1(A);
这应该让您了解如何使用 5 个等距的刻度来实现它:
xTickLabels = t2(floor(linspace(1,t2(end),5)));
xTicks = floor(linspace(1,numel(plot_vals),5));
plot(plot_vals);
set(gca,'XTick',xTicks,'XTickLabel',xTickLabels); % gca gets current axis handle
确定您想要关注的时间点是一门艺术,因为您的蓝色片段不会出现在大小相等的 团块中。
如果你只想显示那些高于阈值的点,你可以使用 logical index and set the value of the unwanted points to NaN
:
threshold = 3.063;
index = (a <= threshold);
a(index) = NaN;
figure;
plot(t1, a);
NaN
的数据点根本不会显示,leaving a break in your plot。这是一个用红色绘制正弦波正点的简单示例:
t = linspace(0, 4*pi, 100);
y = sin(t);
plot(t, y)
hold on;
index = (y < 0);
y(index) = nan;
plot(t, y, 'r', 'LineWidth', 2);
我有一个要分析的数据矩阵。我有一个数据,我应用了一些处理部分,并且在尝试对其应用阈值时设法获得了低于某个水平的一些信息。所以在我应用阈值后,数据变为 0 点。所以我想知道是否有一种方法可以消除这些点而不会在它们之间留下 0。这就是图中带有零的样子
%Find time
N = size(prcdata(:,1),1);
t=T*(0:N-1)';
figure;
plot(t,U);
t1=t(1:length(t)/5);
X=(length(prcdata(:,4))/5);
a = U(1 : X);
threshold=3.063;
A=a>threshold;
plot_vals=a.*A;
figure;
plot(t2,plot_vals1); %gives the plot which i added with this
我也试过这段代码来合并没有零的事件,但它给我的只是一个 0 处的直线图。
%% Eliminate the rows and colomns which are zero
B1=plot_vals1(plot_vals1 <= 0, :);
figure;
plot(B1);
请问还有什么方法可以把上图的散点取下来?将使用 scatter(t2,plot_vals1);工作?
So will that be possible to just plot the events which are in blue and the time together?
如果发生的时间对您来说不重要,那么以下方法可行:
在 A=a>threshold;
之后,将您的代码更改为
plot_vals=a(A);
figure;
plot(plot_vals);
如果发生时间很重要,那么您可以尝试使用绘图的 'XTick'
和 'XTickLabel'
属性以编程方式设置 x 刻度和标签。
像这样获取对应的兴趣时间:
t2=t1(A);
这应该让您了解如何使用 5 个等距的刻度来实现它:
xTickLabels = t2(floor(linspace(1,t2(end),5)));
xTicks = floor(linspace(1,numel(plot_vals),5));
plot(plot_vals);
set(gca,'XTick',xTicks,'XTickLabel',xTickLabels); % gca gets current axis handle
确定您想要关注的时间点是一门艺术,因为您的蓝色片段不会出现在大小相等的 团块中。
如果你只想显示那些高于阈值的点,你可以使用 logical index and set the value of the unwanted points to NaN
:
threshold = 3.063;
index = (a <= threshold);
a(index) = NaN;
figure;
plot(t1, a);
NaN
的数据点根本不会显示,leaving a break in your plot。这是一个用红色绘制正弦波正点的简单示例:
t = linspace(0, 4*pi, 100);
y = sin(t);
plot(t, y)
hold on;
index = (y < 0);
y(index) = nan;
plot(t, y, 'r', 'LineWidth', 2);