How to make an array with specific matrix values

조회 수: 17 (최근 30일)
Filip Konecny
Filip Konecny 2018년 2월 22일
댓글: Jan 2018년 2월 22일
Hello,
I have a matrix for example like this:
[1 2 3 4 5 6 7 8 9 10;
2 2 3 3 4 4 3 3 2 2]
The first column represents time, the second ID of time. I need to make an array where there is first time value from the first group of ID of time for each groups!. So the result looks like this: [1 3 5 7 9] Unfortunately unique function is not helpful in this case.
Thank you

채택된 답변

Jan
Jan 2018년 2월 22일
편집: Jan 2018년 2월 22일
I assume this works:
A = [1 2 3 4 5 6 7 8 9 10; ...
2 2 3 3 4 4 3 3 2 2];
B = A(1, [true; diff(A(1, :)) ~= 0]);
(Just a combination of the answers of Birdman and KL - and a transposed input)

추가 답변 (2개)

Birdman
Birdman 2018년 2월 22일
편집: Birdman 2018년 2월 22일
idx=[(diff(A(:,2))==0);false];
A(idx,1)
  댓글 수: 5
Birdman
Birdman 2018년 2월 22일
Thanks again Jan :) I am learning from you.
Jan
Jan 2018년 2월 22일
Fine :-) And perhaps some readers in the forum will learn from all these discussions also. I have learned today, where the boolean function belongs to, because I did not know it before.

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


KL
KL 2018년 2월 22일
I assume you have a matrix of two columns,
A = [1 2 3 4 5 6 7 8 9 10;2 2 3 3 4 4 3 3 2 2]';
A(diff(A(:,2))==0,1)
ans =
1
3
5
7
9

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by