How to make an array with specific matrix values
Afficher commentaires plus anciens
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
1 commentaire
Jan
le 22 Fév 2018
Do not post multiple threads for one problem. See https://www.mathworks.com/matlabcentral/answers/384215-how-to-make-array-with-first-values-of-matrix
Réponse acceptée
Plus de réponses (2)
idx=[(diff(A(:,2))==0);false];
A(idx,1)
5 commentaires
Jan
le 22 Fév 2018
boolean is not useful here, because diff(A(:,2))==0) is a logical already. By the way: boolean is a MuPAD command, better use logical for Matlab arrays.
Birdman
le 22 Fév 2018
I edited it, thanks. :)
Jan
le 22 Fév 2018
@Birdman: diff(A(:,2))==0 is a logical already, because it is the result of a comparison operator. Then there is no need to cast it to a logical.
Birdman
le 22 Fév 2018
Thanks again Jan :) I am learning from you.
Jan
le 22 Fév 2018
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
le 22 Fév 2018
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
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!