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 件のコメント
回答 (1 件)
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
0 件のコメント
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!