フィルターのクリア

Finding the first and the second repetetion of a value in a matrix

13 ビュー (過去 30 日間)
Iremsu Savas
Iremsu Savas 2019 年 1 月 27 日
コメント済み: Iremsu Savas 2019 年 1 月 27 日
Hello,
I have a matrix that contains 1,2,3,4 values repetitively I want to find the first and second occurences for all 1 2 3 and 4. My original matrix originally is more complex but to show you how it looks like here is a basic example:
1 *
1 *
2 *
3 *
2 *
4 *
3 *
4 *
1
1
4
3
2
Basically I want to find the ones marked with (*). Is there a basic way to perform this?
Thank you
  2 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 27 日
編集済み: madhan ravi 2019 年 1 月 27 日
What should happen if the number only appears once?
Iremsu Savas
Iremsu Savas 2019 年 1 月 27 日
It cant because of the previous code section, it has to repeat

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

採用された回答

madhan ravi
madhan ravi 2019 年 1 月 27 日
x=[1
1
2
3
2
4
3
4
1
1
4
3
2];
X=unique(x);
n=numel(X);
N=2; % first two repetitions
R=cell(n,1);
for k = 1:n
idx=find(x==X(k));
if numel(idx)<N
R{k}=idx(1:N-1); % if the numbers appears once then only the first instance is stored hence the cell array
else
R{k}=idx(1:N); % each cell represents the first two repetitions of each numbers 1, 2, 3 & 4 in ascending order
end
end
celldisp(R)
Gives:
R{1} =
1
2
R{2} =
3
5
R{3} =
4
7
R{4} =
6
8

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by