How do I remove elements of an array from another array?

5 ビュー (過去 30 日間)
Loren99
Loren99 2022 年 7 月 12 日
編集済み: Dyuman Joshi 2022 年 9 月 17 日
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
Dyuman Joshi 2022 年 7 月 12 日
Do you want to compare the whole row, or element from either row?
Loren99
Loren99 2022 年 7 月 12 日
@Dyuman Joshi the whole row

サインインしてコメントする。

採用された回答

Dyuman Joshi
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 件のコメント
Loren99
Loren99 2022 年 7 月 12 日
編集済み: Loren99 2022 年 7 月 12 日
@Dyuman Joshi doing this I do not obtain errors and I obtain the row in which I am interested and that I want to remove from Pp and Sp. However I do not know how to remove it from Pp and Sp
for k = 1:size(allpoint_i,1)
countP = ismember(Pp, allpoint_i(k,:),'rows');
sumP(k) = sum(countP); %mi conta quante volte il punto i-esimo interno è primo punto di un segmento
countS = ismember(Sp, allpoint_i(k,:),'rows');
sumS(k) = sum(countS); %mi conta quante volte il punto i-esimo interno è secondo punto di un segmento
if ((sumP(k)+sumS(k))<=2) %togli la riga corrispondente da Pp e da Sp
rowp=find((Pp(:,1)==allpoint_i(k,1)) & (Pp(:,2)==allpoint_i(k,2)));
rows=find((Sp(:,1)==allpoint_i(k,1)) & (Sp(:,2)==allpoint_i(k,2)));
end
end
For example, for k=723, I obtain an element for which sumP+sumS < = 2 . so I want to remove it from Pp and Sp. With my commands I obtain that the 723 element of allpoint_i occupies correctly the 776 row of Pp and the 777 row of Sp. How then can I remove from Pp the entire 776 row (first and second column) and from Sp the entire 777 row (first and second column) and then in addition remove from Pp also the 777 row (because of Sp) and from Sp also the 776 row (because of Pp). Please
Dyuman Joshi
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 ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by