How do I remove elements of an array from another array?
5 次查看(过去 30 天)
显示 更早的评论
Hi everyone! I would like to understand how to remove elements in an array. I post my code and then I explain my problem.
This is my code:
for k = 1:size(allpoint_i,1)
countP = ismember(Pp, allpoint_i(k,:),'rows');
sumP(k) = sum(countP); %it counts how many times the point is first point of a segment
countS = ismember(Sp, allpoint_i(k,:),'rows');
sumS(k) = sum(countS); %it counts how many times the point is end point of a segment
if ((sumP(k)+sumS(k))<=2)
%insert condition to remove that point both from Pp and Sp
end
end
I explain my problem. Let's start from the first element of allpoint_i. For the first element of allpoint_i I have sumP(1) = 1 and sumS(1)=1. So for the if statement, I have to remove that point from Pp and Sp. In Pp it occupies the 4th row, in Sp it occupies the first row and in addition, I would like to remove not only the 4th row of Pp and the first row of Sp, but also the 4th row of Sp and the first row of Pp. How can I do it? I attach a zip file containing the variables Pp, Sp, allpoint_i. Thanks in advance
4 个评论
采纳的回答
Dyuman Joshi
2022-7-12
编辑:Dyuman Joshi
2022-9-17
Try this -
r1=[];r2=[];
for k = 1:size(allpoint_i,1)
countP = ismember(Pp, allpoint_i(k,:),'rows');
sumP(k) = sum(countP); %it counts how many times the point is first point of a segment
countS = ismember(Sp, allpoint_i(k,:),'rows');
sumS(k) = sum(countS); %it counts how many times the point is end point of a segment
if ((sumP(k)+sumS(k))<=2)
%insert condition to remove that point both from Pp and Sp
r1=[r1 find(sum(Pp==allpoint_i(k,:),2))'];
r2=[r2 find(sum(Sp==allpoint_i(k,:),2))'];
end
end
Pp([r1 r2],:)=[];
Sp([r1 r2],:)=[];
4 个评论
Dyuman Joshi
2022-7-12
I think you skipped over my last 2 lines of code.
%Deleting the rows
Pp([r1 r2],:)=[];
Sp([r1 r2],:)=[];
更多回答(0 个)
另请参阅
类别
在 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!