How would I remove multiple specific elements from an array.

26 ビュー (過去 30 日間)
Oliver Ries
Oliver Ries 2023 年 5 月 24 日
コメント済み: Image Analyst 2023 年 6 月 10 日
The elements I want to remove from array B are displayed from r(i). The current code at the bottom only gets rid of one of the values returned from r(i), but it returns 2 values and I need both removed from the array without just removing them specifically at their location incase entering a new data set changes the locations of the elements I am getting rid of. Thanks!
Data = [923 916 932 927 908 931 953 926 919 963];
B = sort(Data,'ascend');
n = numel(B);
xmin = B(1);
xmax = B(n);
Q1 = round(.25*(n+1));
Q3 = round(.75*(n+1));
atQ1 = B(Q1);
atQ3 = B(Q3);
IQR = atQ3 - atQ1;
MO = 1.5*IQR;
for i = 1:length(B)
if B(i) <=(atQ1-MO) || B(i) >=(atQ3+MO)
r(i) = B(i);
disp(r(i));
end
end
g = B(B~=r(i));
display(g);

回答 (3 件)

Selena Mastrodonato
Selena Mastrodonato 2023 年 5 月 24 日
If I have understood correctly, you could replace your second-last line of code with B(B==r) = []
Data = [923 916 932 927 908 931 953 926 919 963];
B = sort(Data,'ascend');
n = numel(B);
xmin = B(1);
xmax = B(n);
Q1 = round(.25*(n+1));
Q3 = round(.75*(n+1));
atQ1 = B(Q1);
atQ3 = B(Q3);
IQR = atQ3 - atQ1;
MO = 1.5*IQR;
for i = 1:length(B)
if B(i) <=(atQ1-MO) || B(i) >=(atQ3+MO)
r(i) = B(i);
disp(r(i));
end
end
953 963
B(B==r) = []
B = 1×8
908 916 919 923 926 927 931 932
So that you delete all the elements present in r
  2 件のコメント
Oliver Ries
Oliver Ries 2023 年 5 月 24 日
this doesnt seem to be working for me.
Oliver Ries
Oliver Ries 2023 年 5 月 24 日
I think what is happening is my for loop finds the first value but then replaces it when it runs again with the next value. How do I save all the values into one array.

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


Torsten
Torsten 2023 年 5 月 24 日
編集済み: Torsten 2023 年 5 月 24 日
Data = [923 916 932 927 908 931 953 926 919 963];
B = sort(Data,'ascend');
n = numel(B);
xmin = B(1);
xmax = B(n);
Q1 = round(.25*(n+1));
Q3 = round(.75*(n+1));
atQ1 = B(Q1);
atQ3 = B(Q3);
IQR = atQ3 - atQ1;
MO = 1.5*IQR;
g = B(B>(atQ1-MO) & B<(atQ3+MO))
g = 1×8
908 916 919 923 926 927 931 932
  2 件のコメント
Oliver Ries
Oliver Ries 2023 年 5 月 24 日
This worked. Its amazing how much more simple this one is.
Image Analyst
Image Analyst 2023 年 6 月 10 日
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.

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


Steven Lord
Steven Lord 2023 年 5 月 24 日
I would use the rmoutliers function with the "quartiles" method.
Data = [923 916 932 927 908 931 953 926 919 963];
g2 = sort(rmoutliers(Data, "quartiles"))
g2 = 1×8
908 916 919 923 926 927 931 932

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by