omit rows of matrix based on a condition
2 次查看(过去 30 天)
显示 更早的评论
Hello
My matrix B is :
[0.25 0 0;
0.5 -4.91 -4.9;
0.75 -4.94 -4.96;
1 -4.985 -5;
1.25 -5 -5;
1.5 -5 -5;
1.75 -5 -5]
How to write a code to omit all the rows (except from first row and fifth row 1.25,-5,-5) where column 2 and three are equal. I mean keep the first and fifth row where column 2 and 3 are equal but omit other rows with the same situation.
0 个评论
采纳的回答
David Hill
2022-10-15
编辑:David Hill
2022-10-15
Seems like there are more than just two rows.
B=[0.25 0 0;
0.5 -4.91 -4.9;
0.75 -4.94 -4.96;
1 -4.985 -5;
1.25 -5 -5;
1.5 -5 -5;
1.75 -5 -5];
[~,idx]=unique(B(:,2),'stable');
C=B(idx,:);
C(C(:,2)~=C(:,3),:)=[];
C
2 个评论
David Hill
2022-10-15
B=[0.25 0 0;
0.5 -4.91 -4.9;
0.75 -4.94 -4.96;
1 -4.985 -5;
1.25 -5 -5;
1.5 -5 -5;
1.75 -5 -5];
[~,idx]=unique(B(:,2),'stable');
C=B(idx,:)
更多回答(2 个)
Torsten
2022-10-15
B = [0.25 0 0;
0.5 -4.91 -4.9;
0.75 -4.94 -4.96;
1 -4.985 -5;
1.25 -5 -5;
1.5 -5 -5;
1.75 -5 -5];
B = B(union(find(B(:,2)~=B(:,3)),[1 5]),:)
0 个评论
Ghazwan
2022-10-15
%If you already know that you need to omit the first and last row only
A=A(2:end-1,:);
%If you do not know which rows have the condition
for ii=1:length(A(:,1))
if A(ii,2)==A(ii,3) %Your condition
A(ii,:)=[]; %omitting the row that meets the condition
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!