필터 지우기
필터 지우기

Create a vector with the number of repetitions

조회 수: 1 (최근 30일)
Andes
Andes 2017년 2월 5일
댓글: Andes 2017년 2월 6일
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.

채택된 답변

the cyclist
the cyclist 2017년 2월 5일
편집: the cyclist 2017년 2월 5일
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)'

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by