How do I organize repetitive values within a vector into a new vector that only has one of every repetitive value?
4 views (last 30 days)
Show older comments
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 Comment
Answers (1)
Daniel M
on 6 Nov 2019
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
0 Comments
See Also
Categories
Find more on Title in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!