how to delete a row if a number is repeated 4 times or 3 times

1 回表示 (過去 30 日間)
Sun Heat
Sun Heat 2021 年 7 月 1 日
コメント済み: Mili Goyal 2021 年 7 月 7 日
i have a matrix M=65005*5
there are certain rows in which number is repeated twice, thrice or four times. i want to delete that row from matrix.
for example
M=[1 1 1 1 16; 2 5 6 4 16; 2 2 2 6 5];
if 4 (because there is 4 repested number in 1st row) is written than 1st row will be deleted
when 3 (there is 3 repeated number in 3rd row) is written than 3rd row will be deleted.
Thanks in advance

回答 (1 件)

Mili Goyal
Mili Goyal 2021 年 7 月 1 日
Checking for the repeating entries in every row and then deleting itcan be one of the straightforward approach. Following is the code for the same.
%% Code
i = 1;
while i <= size(M,1)
if(length(M(i, :)) ~= length(unique(M(i, :)))) % checking for the repeated entry.
M(i, :) = []; % deleting the row.
else
i = i+1;
end
end
%%
The above will return M = [2 5 6 4 16] as the ans for the example M given in the question.
  4 件のコメント
Sun Heat
Sun Heat 2021 年 7 月 1 日
Yes mili.... thanks for reply The first question ask by you is my major concern..."So you mean that if 4 is given as input, the first row should be deleted?"
Mili Goyal
Mili Goyal 2021 年 7 月 7 日
Did you try storing the number of occurances of repeating numbers for all the rows in an array and then iterating through them depending upon the input and deleting rows?

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

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by