如何从矩阵中删除坐标特定条目?
How to remove coordinate specific entries from a matrix?
我无法在 Octave/Matlab 中实现这个(看似)简单的任务。
我想删除一组二维数据的特定条目。我有示例数据(x 和 y 坐标点),其中包含不应进一步处理的特定区域。我想从我的示例数据中删除这些区域。
这里有一个例子可以进一步理解我想要实现的目标。我想要:
B = A 除了红色矩形中的数据
代码示例:
x = 0:pi/64:4*pi;
y = sin(x);
A = [x; y];
% Boundaries of the data that should be deleted
x1 = 4;
x2 = 6;
y1 = -1;
y2 = -0.5;
figure;
hold on;
plot(A(1,:),A(2,:),'bo');
plot([x1 x2 x2 x1 x1],[y1 y1 y2 y2 y1],'r-');
我知道如何select红色矩形内的数据,可以用这个命令完成:
indices = find(A(1,:)>x1 & A(1,:)<x2 & A(2,:)>y1 & A(2,:)<y2);
B(1,:) = A(1,indices);
B(2,:) = A(2,indices);
plot(B(1,:),B(2,:),'g-x');
但我需要相反的:Select红色矩形外的数据。
感谢任何帮助。
反转定义索引的语句中的所有运算符(即 > 变为 <,反之亦然,AND[ & ] 变为 OR[ | ])。
indices2 = find(A(1,:)<x1 | A(1,:)>x2 | A(2,:)<y1 | A(2,:)>y2);
B=A(:,indices2);
plot(B(1,:),B(2,:),'g-x');
使用逻辑数组来select数据
管理 selection 的一种非常方便的方法是使用逻辑数组。这比使用索引更快,并且允许轻松反转 select 离子以及多个 select 离子的组合。这里是修改后的 selection 函数:
sel = A(1,:)>x1 & A(1,:)<x2 & A(2,:)>y1 & A(2,:)<y2
结果是一个逻辑数组,它是一种非常节省内存且快速的信息表示形式。
有关输出的图形表示,请参阅 figure。
代码示例
x = 0:pi/64:4*pi;
y = sin(x);
% Combine data: This is actually not necessary
% and makes the method more complicated. If you can stay with x and y
% arrays formulation becomes shorter
A = [x; y];
% Boundaries of the data that should be deleted
x1 = 4;
x2 = 6;
y1 = -1;
y2 = -0.5;
% Select interesting data
sel = A(1,:)>x1 & A(1,:)<x2 & A(2,:)>y1 & A(2,:)<y2;
% easily invert selection with the rest of data
invsel = ~sel;
% Get selected data to a new array
B1 = A(:,sel);
B2 = A(:,invsel);
% Display your data
figure;
hold on;
plot(B1(1,:),B1(2,:),'bo');
plot(B2(1,:),B2(2,:),'rx');
% Plot selection box in green
plot([x1 x2 x2 x1 x1],[y1 y1 y2 y2 y1],'g-');
图形结果
我无法在 Octave/Matlab 中实现这个(看似)简单的任务。
我想删除一组二维数据的特定条目。我有示例数据(x 和 y 坐标点),其中包含不应进一步处理的特定区域。我想从我的示例数据中删除这些区域。
这里有一个例子可以进一步理解我想要实现的目标。我想要:
B = A 除了红色矩形中的数据
代码示例:
x = 0:pi/64:4*pi;
y = sin(x);
A = [x; y];
% Boundaries of the data that should be deleted
x1 = 4;
x2 = 6;
y1 = -1;
y2 = -0.5;
figure;
hold on;
plot(A(1,:),A(2,:),'bo');
plot([x1 x2 x2 x1 x1],[y1 y1 y2 y2 y1],'r-');
我知道如何select红色矩形内的数据,可以用这个命令完成:
indices = find(A(1,:)>x1 & A(1,:)<x2 & A(2,:)>y1 & A(2,:)<y2);
B(1,:) = A(1,indices);
B(2,:) = A(2,indices);
plot(B(1,:),B(2,:),'g-x');
但我需要相反的:Select红色矩形外的数据。
感谢任何帮助。
反转定义索引的语句中的所有运算符(即 > 变为 <,反之亦然,AND[ & ] 变为 OR[ | ])。
indices2 = find(A(1,:)<x1 | A(1,:)>x2 | A(2,:)<y1 | A(2,:)>y2);
B=A(:,indices2);
plot(B(1,:),B(2,:),'g-x');
使用逻辑数组来select数据
管理 selection 的一种非常方便的方法是使用逻辑数组。这比使用索引更快,并且允许轻松反转 select 离子以及多个 select 离子的组合。这里是修改后的 selection 函数:
sel = A(1,:)>x1 & A(1,:)<x2 & A(2,:)>y1 & A(2,:)<y2
结果是一个逻辑数组,它是一种非常节省内存且快速的信息表示形式。
有关输出的图形表示,请参阅 figure。
代码示例
x = 0:pi/64:4*pi;
y = sin(x);
% Combine data: This is actually not necessary
% and makes the method more complicated. If you can stay with x and y
% arrays formulation becomes shorter
A = [x; y];
% Boundaries of the data that should be deleted
x1 = 4;
x2 = 6;
y1 = -1;
y2 = -0.5;
% Select interesting data
sel = A(1,:)>x1 & A(1,:)<x2 & A(2,:)>y1 & A(2,:)<y2;
% easily invert selection with the rest of data
invsel = ~sel;
% Get selected data to a new array
B1 = A(:,sel);
B2 = A(:,invsel);
% Display your data
figure;
hold on;
plot(B1(1,:),B1(2,:),'bo');
plot(B2(1,:),B2(2,:),'rx');
% Plot selection box in green
plot([x1 x2 x2 x1 x1],[y1 y1 y2 y2 y1],'g-');