Effacer les filtres
Effacer les filtres

Create a vector with the number of repetitions

1 vue (au cours des 30 derniers jours)
Andes
Andes le 5 Fév 2017
Commenté : Andes le 6 Fév 2017
Hello, I wouldlike to create a vectorwith the number of repititions of a number for example, I have this data:
clear all
clc
data = [ 1 2004 3 5;
2 2004 7 2;
1 2005 9 4;
2 2005 6 5;
3 2005 3 7;
4 2005 8 2] % Where data(:,1) is id and data(:,2) is year.
I would like to create a code that counts the numbers of id per year in a vector, just like this:
result = [2 2 4 4 4 4]';
thank you very much in advance.

Réponse acceptée

the cyclist
the cyclist le 5 Fév 2017
Modifié(e) : the cyclist le 5 Fév 2017
I feel like there must be a better way, but this will work
data = [ 1 2004 3 5;
2 2004 7 2;
1 2005 9 4;
2 2005 6 5;
3 2005 3 7;
4 2005 8 2 ] % Where data(:,1) is id and data(:,2) is year.
dataYear = data(:,2);
uniqueDataYear = unique(dataYear);
dataYearCount = histcounts(dataYear,[uniqueDataYear; Inf])
[~,loc] = ismember(dataYear,uniqueDataYear);
result = dataYearCount(loc)'
  1 commentaire
Andes
Andes le 6 Fév 2017
Great, thanks a lot!! it works great!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by