How do I organize repetitive values within a vector into a new vector that only has one of every repetitive value?

조회 수: 1 (최근 30일)
I have a vector that has years since 2003 listed until 2019. Each Year value repeats itself 12 times to represent each month within that year. I would like to plot my years on an x-axis. How do I go about making sure only one value of each year (ie. one 2003, one 2004...) shows on my axis?
  댓글 수: 1
Bhaskar R
Bhaskar R 2019년 11월 6일
편집: Bhaskar R 2019년 11월 6일
You want to plot each year's data i.e 12 months data for the year 2003 to 2019? Can you provide variable size/structure?

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

답변 (1개)

Daniel M
Daniel M 2019년 11월 6일
Your title and your description ask two very different questions. Your description is basically how to use XTick and XTickLabel properties. But I have decided to answer the question in the title.
Because of a slight ambiguity in the wording, here are two answers.
If you want to keep all the unique values in a vector, then do:
% sample data
x = [2 2 3 2 4 1 4 4 4 4 4 6 7 7 1];
uniqueX = unique(x,'stable');
% using 'stable' maintains the original order of x, but removes repeated values.
If you want a vector of JUST the repeated values, and only one value each, then do:
% sample data
x = [2 2 3 2 4 1 4 4 4 4 4 6 7 7 1];
dupes = x; % make a copy
[~,locUniqueX] = unique(x,'stable'); % get the locations of all unique values in x
dupes(locUniqueX) = []; % remove one of every number in X
% All that remains in dupes are the non-unique values in x
uniqueDupes = unique(dupes,'stable'); % only keep one of each

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by