필터 지우기
필터 지우기

unique values in array

조회 수: 1 (최근 30일)
Jim O'Doherty
Jim O'Doherty 2013년 5월 9일
Hi all,
I'm sure this is probably very easy but I can't seem to crack it. I've got an array like so:
timing =
10
5
5
5
5
5
5
5
5
5
5
10
10
10
10
10
10
20
20
20
60
60
60
60
60
30
30
10];
I'd like to produce a table of the data which shows the number of entries, i.e.
1 x 10
10 x 5
6 x 10
3 x 20
5 x 60
3 x 30
1 x 10
This array changes length (and values) each time I run my code.
Using the "unique" command would have this done in a line of code, but the fact that same value repeats itself later in the array is making it a lot more hassle!
Any help greatly appreciated
Thanks Jim

채택된 답변

Grzegorz Knor
Grzegorz Knor 2013년 5월 9일
Simply idea is to use loop:
f = [1 , timing(1)];
for k=2:numel(timing)
if timing(k)~=timing(k-1)
f(end+1,2)=timing(k);
end
f(end,1) = f(end,1)+1;
end
  댓글 수: 2
Jim O'Doherty
Jim O'Doherty 2013년 5월 9일
Thanks Grzegorz, that works absolutely perfectly for anything I threw at it
Jim
Grzegorz Knor
Grzegorz Knor 2013년 5월 9일
And another a little tricky solution:
[diff([0;find(diff([timing;rand()]))]) timing(find(diff([timing;rand()])))]

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by