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

2 visualizaciones (últimos 30 días)
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 comentario
Bhaskar R
Bhaskar R el 6 de Nov. de 2019
Editada: Bhaskar R el 6 de Nov. de 2019
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?

Iniciar sesión para comentar.

Respuestas (1)

Daniel M
Daniel M el 6 de Nov. de 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

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by