Removing values so two vectors are the same length

9 views (last 30 days)
Determine how to remove values from the time vector which correspond exactly with those removed from the data vector, so these two vectors are synchronized.
Essentially we need to construct a code which synchronizes the two data sets which have different lengths. Been working on this for a week and have gotten nowhere.
With the code constructed already, this converted error flagged outlier sea-surface temperature data into zeros and then with another line of code this removed the zeros from the data so the length of this data is now less than the data we have for time. I know the code process to flag and convert the time data into zeros then remove those zeros but the problem lies in establishing a code which will remove values from the time vector which correspond exactly with data removed from the sea-surface temperature data vector.

Answers (2)

Star Strider
Star Strider on 17 Oct 2017
If I understand correctly what you want to do, something like this will work:
time = 1:15; % Create ‘time’ Vector
sst = rand(size(time)); % Create ‘sea surface temperature’ Vector
outlier_idx = sst > 0.9; % Logical Vector Identifying Outliers
new_time = time(~outlier_idx); % Keep ‘time’ For Accepted Data
new_sst = sst(~outlier_idx); % Keep ‘sst’ For Accepted Data
The negation ‘~’ operator here keeps the elements not identified as outliers in ‘outlier_idx’.
  4 Comments
Star Strider
Star Strider on 17 Oct 2017
That requires only a slight variation on my code:
x = [2, 5, 0, 7, 6, 0];
y = [1, 2, 3, 4, 5, 6];
remove_idx = x == 0;
new_x = x(~remove_idx)
new_y = y(~remove_idx)
new_x =
2 5 7 6
new_y =
1 2 4 5
This will work with row and column vectors.
Star Strider
Star Strider on 18 Oct 2017
My pleasure!
If you look at the plot you posted before, note that the y-axis spans the range of 0 to 1000, while your latest plot ranges from about 22 to 34. With the ‘outliers’ eliminated, you are likely seeing your actual data.
One way to verify this is to take the earlier plot and restrict the y-axis limits. Put this set call after the plot call for your original data (that created your earlier plot):
set(gca, 'YLim',[-10 50])
(I don’t know what the actual ranges are, so you may have to adjust the ones I provided.) You will then likely see similar variation in the original data you are now seeing with the ‘outliers’ removed.

Sign in to comment.


KL
KL on 17 Oct 2017
Why don't you put those two vectors (time and data) in table (or even better in a timetable, if you're using 2016 or later) before you process the flags? Then you could simply remove the entire rows that you don't want.

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!