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 个)

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